Adding ModelGlue’s event API to ColdFusion Builder’s code insight

For the last year or so, all of my ColdFusion development projects have been built using the Model-Glue MVC framework. Over time, you get to where you know the most commonly used methods that are used to interact with the framework by heart. Sometimes there are lesser-used methods that you have to go look up. All the time (for me at least) I’m trying to find a way to write code faster and with less errors.

ColdFusion Builder has done a very nice job of providing code insight for ColdFusion tags, functions and CFC methods. This is especially true if you have a server configured in the “Servers” panel and mapped to your CF Builder project as it then will provide code insight for your own CFCs that you create on the page. However, when using Model-Glue, the “event” object is created for you and is always there. Because it’s not explicitly created on the page, CF Builder can’t provide code insight when you need to interact with it. However, it only takes a couple of settings in your project to make CF Builder aware of the event object and start providing help for it.

Here’s the process:

  1. Right click on your project and choosing “Properties”.
  2. In the left pane of the window that comes up, click on “ColdFusion Variable Mappings”.
  3. On the right side, click the “New” button and enter the following values into the boxes
    • Variable Name: event
    • Mapped To: html.ModelGlue.gesture.eventrequest.EventContext
  4. Press the “New” button and enter the following values into the boxes
    • Variable Name: arguments.event
    • Mapped To: html.ModelGlue.gesture.eventrequest.EventContext
  5. Press the Apply button then the OK button

    * Note that the value in the “Mapped To” box is the actual dot-notated path to the EventContext.cfc file from your CF Builder project root. I happen to have my webroot files in a folder named “html” under the project root (see screenshot #1 below).

    Once you have those settings saved, any time you type “event.” or “arguments.event.” you’ll get the list of methods contained in the Event object. Of course, this doesn’t only work with Model-Glue. Any CFC that you regularly use the same name with can be configured this same way.

    I’ve attached some screenshots for reference. If you have any questions or something isn’t working, feel free to ping me.

jQuery head-scratcher and lesson learned

As I mentioned in my last post, I’m working on a pet project in my spare time. It uses jQuery in various places including in the site design template that I purchased to use with the site. The template uses jQuery to expand and collapse menu items in the left sidebar to show sub items for that selection. Because of this, jQuery and a javascript file named custom.js was included in the template. After breaking the template apart to work inside my ModelGlue application, I started implementing some other features that used jQuery with their own associated javascript files, one of which was cfUniform.

As soon as I put the cfUniform code into the page, I started getting javascript errors in the console pane of Firebug. The error would state something similar to “$(document) not a function” or “$ not a function”. Now, I’ve not had a ton of experience with jQuery, but I have used several pre-built jQuery plugins in sites before and I had seen errors similar to this. Normally this error is caused by one of two issues. Either a) you’ve forgotten to include the script block to load the jQuery library or b) your code is loading the jQuery library twice.

I was able to use Firebug to verify that I was indeed loading it and loading it only once but couldn’t for the life of me figure out why I was getting an error. Obviously cfUniform wasn’t really at fault (the error pointed to a line in one of the cfUniform javascript files) so I knew it had to be something on my side. I did some searching on the phrase and found some discussions around jQuery’s noConflict() feature that allows you to reference jQuery with a notation other than using the familiar “$”.

After reading for a while, I opened the custom.js file that came with the site template and found the code below:

   //contents snipped

Since I’m not using any other javascript libraries in this application that might conflict with using the “$” to access jQuery, I removed the noConflict() line, but that didn’t fix my problem. On a hunch I did a search/replace through the custom.js file replacing “jQuery(” with “$(” so that references to the jQuery library in this file would be accessed with the same syntax as in all the other javascript files. Lo and behold, all my errors in Firebug’s console went away and CFUniform began behaving as expected.

While I don’t understand all the underpinnings of why this worked, I’ll take it as my “lesson for the day” that in the future I need to always make sure that all the various jQuery plugins and code that is used in my applications need to reference the jQuery library with the same syntax.

ColdFusion 9 caching settings to watch out for

Like a lot of developers, I’ve got this pet project I’m working on in whatever spare time I can find between client engagements, home maintenance, family obligations, etc. I’m using it as an opportunity to work with some of the new features of ColdFusion 9 (ORM mainly), ColdFusion Builder Beta and features in development for the next release of ModelGlue 3.

Recently, Bob Silverburg has been working on a significant overhaul of the scaffolding feature used by ModelGlue to automatically create CRUD forms for the various data objects in your application. Particularly exciting to me is that you can now override the built-in code templates with your own. Bob wrote a proof-of-concept application that uses the excellent cfUniform custom tag library to build standardized forms and validations (see my previous post on cfUniform if you’re not familiar with it). Since I’m pretty particular about how my project files are arranged, I proceeded to place the css, javascript and image assets into the folders where I wanted them and use ColdSpring to create a configuration bean to pass to cfUniform when I called it. That’s where the trouble began.

I had to make a couple of changes to the code generated in the custom scaffold CFC in order to have cfUniform see the custom configuration that I had set up. No matter what I did, when the scaffolding engine generated the code for the view and the XML fragment for the event-handler, the changes I made inside the CFC weren’t included. I spent a couple hours scratching my head, tracing the request cycle, restarting my local ColdFusion instance and always got the exact same code that was in Bob’s original example CFC. Finally, I decided to change the name of the CFC and update the associated bean configuration in ColdSpring. On the next refresh, I saw my changes reflected in the code generated by the scaffold!

With that in mind, I checked the settings on the Caching page of that instance’s CF Administrator. Sure enough, the Cache Template In Request, Component cache, and Save class files options were checked. I cleared those check boxes, pressed the Clear Template Cache Now and Clear Component Cache Now buttons below and have had no trouble since. Obviously there are situations where you want these enabled, but rarely ever should they be needed on a local system being used for development.

So, the moral of my painful story–if you’re making changes to code that’s not being reflected when you test browse your application, don’t forget to check the settings on the Caching page in CF Admin. It just might save you a couple hours and a few gray hairs.

Want to learn ModelGlue….for (nearly) free?

Have you ever been curious about building CFML applications using the ModelGlue framework but just not had time to sit down and learn it on your own? If your answer is yes, then October 24th is your lucky day my friend. That’s the date for the BFusion half of the BFusion/BFlex conference in Bloomington, IN.

Dan Wilson will be giving an all-day training session on ModelGlue. Dan is the managing director of the ModelGlue project so you’ll be getting training from one of the main folks responsible for the framework. I have the honor of filling the role of teaching assistant for the class and I am really excited about the event.

Cost for BFusion/BFlex is $10 per person per day so it’s nearly as close to free training as you’re going to find. Registration is not open as of this writing, so be sure to watch the BFusion/BFlex site for more details as they become available.

ModelGlue 3 first impressions from an MG newbie

For the last 5 weeks, I’ve been working on a proof-of-concept application for a client using ModelGlue Gesture. This was my first “real” experience with ModelGlue. My only other exposure to ModelGlue came from watching a Breeze (as it was called then) session from Joe Rinehart where he built an entire blogging application in 7 minutes (or some equally ridiculously short amount of time). At the time, frameworks and design patterns hadn’t “clicked” with me and I struggled mightily trying to understand the concepts of what was going on under the hood. Needless to say, I didn’t pursue ModelGlue for very long due to my lack of understanding and the usual pressing project schedules.

Since that time, I’ve had the opportunity to get some formal training in MVC frameworks (specifically MachII) and have built a few applications using MachII. Along the way, the reasons for using an MVC design pattern have become much clearer and I’m now comfortable thinking about applications in the respective tiers that MVC frameworks encourage you to develop with. Having all my MVC experience in MachII up to now, I was really excited to have the chance to build an application in ModelGlue, and be able to work alongside Dan Wilson as we built it. It’s not often that you get the chance to learn a technology by working with one of the folks primarily responsible for the technology.

Read More