flashMessages() in ColdFusion on Wheels
This is a quick test of the
flashMessages() function coming up in
version 1.1 of the ColdFusion on Wheels framework.
Setting Up Test Flash Messages in the Controller
In my controller, I am just adding a couple keys for success and error
into the Flash. Messages stored in the Flash can be referenced on the next page request only.
1flashInsert(success="This is a success message.");
2flashInsert(error="This is an error message.");
Results When Calling
flashMessages() in the View
When I output
flashMessages(), Wheels generates the equivalent of this markup for the example above:
1<div class="flash-messages">
2 <p class="error-message">This is an error message.</p>
3 <p class="success-message">This is a success message.</p>
4</div>
This is pretty cool. Let me tell you why.
Why This Matters: Layouts
I've found that I tend to do the equivalent of this in most every layout that I've created for a Wheels app:
1<cfoutput>
2
3<!--- Display error message from the Flash --->
4<cfif flashKeyExists("error")>
5 <div class="error-message">
6 #flash("error")#
7 </div>
8</cfif>
9
10<!--- Display success message from the Flash --->
11<cfif flashKeyExists("success")>
12 <div class="success-message">
13 #flash("success")#
14 </div>
15</cfif>
16
17</cfoutput>
(In some cases, I've broken this logic out into partials too, but that's beside the point.)
This is freakin' amazing because I can replace all of that code with this single line:
1<cfoutput>
2 #flashMessages()#
3</cfoutput>
It just so happens that Wheels's naming conventions for the classes are the same as mine,
which goes to show what good taste that the framework has for naming things. ;)
Limiting Which Keys Get Displayed
One thought that comes to mind is, What if there are some keys that I want to limit this to?
Maybe a particular section of your layout is only designed to handle keys for success
and error. After all, another part of your application could end up adding unexpected
keys to the Flash.
Well, when you call
flashMessages(), you can pass as keys a list
of the only keys that you want to display.
So if I only wanted to display the success key if it exists, I could pass it as an
argument like this:
1<cfoutput>
2 #flashMessages("success")#
3</cfoutput>
Controlling the Order of the Keys
If you don't pass a value for keys, Wheels will display them in alphabetical order.
But if you pass a list of keys, Wheels will honor the order that you specify.
Here's an example where the success message will appear before the alert
message (if both exist in the Flash).
1<cfoutput>
2 #flashMessages("success,alert")#
3</cfoutput>
Remember, if you don't pass the list, Wheels will display alert before
success.
Quick Test: camelCasing
A question came to mind: how does Wheels handle the HTML
class name if I camelCase my Flash keys. Let's see what happens if my key is called
errorMessage instead. (Hey, this site is called Labs after all!)
1flashInsert(errorMessage="This is another error message.");
Which produces this code:
1<div class="flash-messages">
2 <p class="errorMessage-message">
3 This is another error message.
4 </p>
5</div>
Looks like it keeps the camelCase in place. Plus errorMessage-message is kind of
awkward anyway. If anything, this is reason to think for a second about what you're naming
your Flash keys if you plan on using
flashMessages().

