Home > ColdFusion, Web Application Development > Learning CFWheels – Keeping it DRY with Partials

Learning CFWheels – Keeping it DRY with Partials

I’m relatively new to CFWheels, but not CF.  In my fusebox days, I would use the same form for both the create and edit, living by the credo DRY(don’t repeat yourself).  I would simply add logic that checks to see if the id is greater-than zero.  If yes, it’s an update, otherwise it’s an insert.  I could not figure out how to achieve this in wheels, even after a bit of poking around.  Reading over a post by Mike Henke, I learned about the concept of partials.  Chris Peters was nice enough to send me a couple of blog posts on the topic.

A partial is the CFWheels way of doing a cfinclude.  Just like a cfinclude, any variables declared in the parent page are available to the included page. So in my application, I just take the contents of my form between the #startFormTag()# and the #submitTag()# and place it in a separate file that begins the the underscore. In my case that file is named _conference.cfm. It needs to be in the same views folder as the add and edit view files. The naming convention using the underscore lets CFWheels know this is a partial. In the add view file, I can now remove the contents of my form between the #startFormTag# and #submitTag()# and add in its place, #includePartial(conference)#. For the edit view file, again I remove everything between the #startFormTag()# and the #submitTag()# and replace it with the #includePartial(conference)#. I then need to add a hidden field for the record id somewhere in the form using #hiddenField(objectName=”objectName“, property=”id“)#.  More or less that’s all there is to it.  If the two forms were working before, they should work now with few exceptions as I found out.  Your controller should already be set up to work for both functions.

I did find a case that is proving to be an obstacle for the moment.  I’ll look at it again in the morning.  My form contains a file upload.  To handle it, I am using the UploadFile, by Anthony Pertuzzi plugin available at the CFWheels site.  I was able to modify my controller to get it working on the add.  The edit however is tripping me up.  It uses mostly the same code as the add.  I notice that while the file seems to upload to the correct location, loc.ret is not being returned in the uploadFunction().  This is causing my file to not be named properly in the update() method of my controller using cfset conference.agendafilename = loc.ret.serverFile.   Not sure why this is happening.

Overall, I like using the partials technique to keep things DRY.  It’s yet another way I can use CFWheels to do things I used to do as a spaghetti programmer, but faster and more cleanly.  And that is what it’s supposed to be about.

Advertisements
  1. August 18, 2010 at 12:47 pm

    I’ve really enjoyed your posts on CfWheels. I started using it a little while back and really enjoy the framework. Looking forward to working with it more as well as reading more of your experiences with it.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: