Bind Error Alert Messages: My Most Despised ColdFusion Feature (And a Solution!)

Published By: Doug Hughes on Dec 17, 2008 at 8:01 AM

Times Viewed: 2625

Categories: None

I usually try to remain fairly positive on this blog and not be outright negative about, well, anything.  But this time I simply don't have any way to look positively on my most despised feature of ColdFusion.  To put it simply, I hate how ColdFusion shows a JavaScript alert every time there's a problem with anything related to cfajax.  

Right now, I'm working on an absolutely insane HTML form. When done it will probably have multi-hundreds of fields.  (Before anyone blasts me on this, there are business processes which require such a complex form and processes are in place to make it less cumbersome.)  This form has a lot of fields which are bound to other fields.  Furthermore, this is a Model-Glue application and if you run into a ColdFusion error an error event handler is displayed, not an inline error message as with traditional ColdFusion development.  This means that every time I get a ColdFusion error all my bindings fail and I have to click through a metric ton of alert messages.  And, to make matters worse, I'm on a Mac where alert mesages slide in from the top of the browser so they take longer.

Today this chaffed me for the last time and I've decided to switch to PHP! (Just Kidding!!)

Actually, I tracked down where CF's JavaScript was showing these bloody alert messages.  This is within a function called handleError within cfajax.js.  In this function the code looks to see if you're logging errors (via that cf ajax logging interface), or if there's a global error handler, and, failing that, it shows an alert message. 

I tracked down the code on the global error handler and it turns out that there's an undocumented feature of the ColdFusion Ajax features that lets you handle errors your own way.  Once I figured that out, redirecting the error messages to the FireBug console became extremely easy with this small chunk of code:

// this overrides the default CF error handling alert message!
ColdFusion.setGlobalErrorHandler(function(error){
    if(typeof console != "undefined"){
        console.log(error);
    } else {
        alert(error);
    }
});

This code should be compatible with all browser configurations as well in that if the console does not exist the alert messages will be shown instead.

7 Comments

Put in the blog title that you have a solution in addition to your rant? FREDTERP

Posted By: FREDTERP on Dec 17, 2008

@FREDTERP - Good call! It's been done.

Posted By: Doug Hughes on Dec 17, 2008

I believe thats a documented feature:

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=JavaScriptFcns_25.html

Posted By: Sam Farmer on Dec 17, 2008

@Sam - Thanks for pointing that out. I had no idea!

Posted By: Doug Hughes on Dec 17, 2008

@Doug: No worries.

Another goodie in the CF JavaScript library that is easy to overlook is:

ColdFusion.getElementValue(elementId [, formId, attributeName])

which returns the value (or text based on the third argument) of any element.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=JavaScriptFcns_04.html

Posted By: Sam Farmer on Dec 17, 2008

Thanks, it is very useful, you make my day!

Posted By: Zerone on Dec 31, 2008

Thanks a ton!! This was the solution I was looking for. You get the up arrow on Google for this one.

Posted By: Richard on May 20, 2009

Add a Comment

Please provide your email address if you want to subscribe to this blog entry. An unsubscribe link is provided in notification emails. Your email address is never shown on this website.
Processing... Please wait
We are adding your comment.