How in one line to extend ColdFusion by writing CFC's
ColdFusion 9There have always been ways to extend ColdFusion through custom tags (and its variations like cfmodule) or by adding a library cfc or by including a file before every page.
I now present another way;
- a way to write a cfc
- place it in a folder
- and call it via the new operator.
And its all done by one setting:
this.customtagpaths = pathToExtensionFolder!
Lets see it in action. First, set up an Application.cfc like so:
Then in the langaugeExtension folder add as many cfc's as you want for my example I included one called Invoke.cfc (yes, I've wanted a script based version of this for a while! ;) ):
In the root folder I created a test.cfm with the following code:
Being in the root folder the first line should always work. Nothing exciting there. But whats cool is line 4 where variable i is set with no path to Invoke(). And it works. Here is a screenshot:

Next I created a sub folder structure to see if this would still work further down the chain.

And here is the code for dirB/test.cfm:
Which works as well! Here is its screenshot:

You can also place extra CFCs in the servers root CustomTags folder and use new on them. I think this is a bad idea. Its better to write applications that can be dropped anywhere and are not dependent on special server set ups.
So a pretty easy way to more natively expand upon the ColdFusion language and while I mention this.customtagpaths above it is also possible thanks to the new operator introduced in 9.
Using ORM Event Handler to easily and automatically add audit information
ColdFusion 9, ORMUsing the event handling features in ORM in ColdFusion 9 makes it easy to automatically add audit information such as last updated and last modified by to all objects, well technically tables, in the database. I'm about to launch my first ColdFusion 9 application and the technique below has worked well and sped up development.
Step 1: Decide on audit colum names
There really is no right or wrong way here. Go for something readable and easily understandable (for those lucky future developers who will look at your code!). Once you pick them add them as properties to your CFC:
Step 2: Turn on Event Handling and Hander
In your Application.cfc in addition to ormEnabled=true, turn on event handling and an event handler like so:
this.ormsettings = { eventHandling=true, eventHandler="myEventHandler" };
The eventHandler points to a valid CFC "myEventHandler.cfc" which is what we will create next.
Step 3: Create an Event Handler CFC (See I told ya!)
{
Step 4: Add code to preInsert and preUpdate functions
The preInsert and preUpdate functions in the event handler CFC are the last functions that get run before ColdFusion hands over to Hibernate for persistence (a fancy way of saying saving to database). All of the functions in eventHandler take in the entity as an argument called entity (if you are new to cfscript within the parenthesis its saying a type of any and an argument called entity). We simply call the various functions and pass in the values. I found I got an error at first matching the session userID to createBy and lastModifiedBy and needed to use JavaCast. [Update Feb 6 '10: This bug is scheduled to be fixed in 9.0.1] After making a change to myEventHandler.cfc run ormReload() (I've gotten used to having this in onRequestStart and commenting it out when not needed).
The preUpdate function is very similar except just has the setLastModifiedBy and setDateModified calls. If all your entities don't have the audit properties/columns use this technique for checking if the function exists.
Lets see it in action.
Using the Team object from above:
Thats all the code needed. Here's a screenshot of the database after insert that shows all the audit data.

2010: Speaking at CFUnited and an Adobe Community Professional!
CFUnited, ColdFusion 9, Adobe Community ProfessionalIn the past few days I've received some pretty exciting news. First I got word that I have been selected to speak at CFUnited on ColdFusion One Liners. I gave this topic at CFinNC and am looking forward to building on the feedback I received and some new ideas for CFUnited this year. I've never given a presentation again so I'm looking forward to a chance to make it better.
Second I got chosen as an Adobe Community Professional. This is a real honor to be amongst a group of community members that I admire a great deal. I'm looking forward to learning from them and passing on as much as possible. The first thing I want to do is create a series of screencasts about my experiences with ColdFusion 9.
Adding ExtJS Effects to cfwindow
ColdFusion 9, ExtJsUnderneath the hood cfwindow uses ExtJS to make modal windows. This means with a little bit of JavaScript its possible to add effects to a cfwindow. First lets create a cfwindow:
Nothing special here. To tie it in with some ExtJS effects we need to get at its inners using the ColdFusion.Window.getWindowObject() function. In the example below once we get the window object we add a listener to it using the shorthand ExtJs notation of "on" to look for when the mouse enters and leaves the window. When the mouse enters we log to the console the id of the element (it can be useful to know the id of it) and then perform a highlight effect on the windowContent div. When the mouse leaves we hide the window.
Thats it! ExtJs has a bunch of cool effects that can be applied to an element.
Here's a short screencast showing the code above in action:
Dynamically injecting data into an Object
ColdFusion, ColdFusion 8, ColdFusion 9
Using StructKeyExists to find if an object has a function
ColdFusion, ColdFusion 8, ColdFusion 9The function setAddress can not be accessed or does not exist






Loading....