Last week I was working on a client’s project that makes heavy use of ColdFusion 9’s ORM features. Everything we’d done with ORM up to that point had been going really well and I continue to be impressed by the amount of time ORM saves me in development. I had gotten to a point in the project where I needed to be able to use AJAX calls from jQuery to manage some of the data in the database. Based on my experiences using a RemoteFacade.cfc to feed data to a Flex app, I thought it should be pretty easy. Several hours later, I realized that my particular use case for this was anything but easy.
Thanks to all the folks that came to my Advanced Workflows with ColdSpring session today at cf.Objective(). The conference this year was outstanding and I’m honored to have been chosen to speak. If you’re interested in getting the slides and code, I’ve uploaded a zip file that includes everything I used in the session.
I’m not sure how I missed this before but among the long list of things added to ColdFusion 9 is the ability to create a CFC using the “New” syntax. Up until now, to create a new object from a CFC we’d use the createObject() method like so
<cfset Team = createObject("component", "model.Team") />
Now, using the New keyword, we can shorten that to
<cfset Team = New model.Team() />
But wait, there’s more!
When you use the New keyword, ColdFusion will automatically look for and run any init() method that exists in the CFC. It also respects any arguments that your init() method specifies, meaning that if you have a Team.init() method that can accept team name, color and manager arguments, you can build them right into your the same line of code used to create your object like so
<cfset Team = New model.Team( "Bumblebees", "Yellow", "Benny Bee" ) />
Likewise, as you’d expect, named arguments are still supported. So if you had to send in arguments out of order or needed to omit some optional arguments at object creation, you could do
<cfset Team = New model.Team( manager="Benny Bee", color="Yellow", name="Bumblebees") />
I personally am going to make a point to start using this in my code because I can never seem to spell the word “component” correctly in the createObject() method.
Well, not quite yet, but since I’m going to be out of pocket the next several days, I wanted to put this out now. Monday, March 22 will mark 10 years since I began my “journey” with ColdFusion. That day in 2000 was my first day in a class called “Fast Track to ColdFusion”.
The class was taught by Sean Hedenskog in San Jose, CA where the headquarters for the company I was working for at the time was located. That company had decided on Allaire Spectra 1.0 as the basis for an enterprise content management system so several of us went through the Fast Track to ColdFusion course in preparation for Spectra training some weeks later.
One of the really nice “fixes” included in ColdFusion 9 from a developer’s perspective is the inclusion of an implicit “local” variable scope into which variables created within the body of a <cffunction> tag are placed by default. Previously, developers had to manually add a “var” keyword to variables that should only exist within the confines of the function.
One of the ways of simplifying this that gained some traction among various developers prior to the release of ColdFusion 9 was to “var” a single variable at the top of the function as an empty structure then store any additional variables needed in the function inside it. Many folks, myself included, named this structure “local” so that it would be readily apparent that the values inside were local to that function. This approach worked fine and dandy on ColdFusion 8 and below.