ModelGlue addResult/setValue Order Matters!
Small But Time Consuming Modelglue Gotcha
Attempting a save action, evaluating the outcome, and adding a subsequent "success" or "Check your Work!" message, along with a Modelglue Result value (in order to redirect appropriately) are common things to do when building an app, right? Well, I burned about fifteen minutes this morning trying to do this simple task, tracing events and double checking code, trying to figure out WHY the message value I added to the Event object wasn't present in the final viewstate.
Let me show you two versions of a snippet from my controller. The first snippet resulted in NO "message" value being present, the second one DID:
Just thought I'd share this in case it saves someone else fifteen minutes.
Let me show you two versions of a snippet from my controller. The first snippet resulted in NO "message" value being present, the second one DID:
<cfif retValresult is "success">
<cfset arguments.event.addResult("success") />
<cfset arguments.event.setValue("message","Your Dream has been Saved!") />
<cfelse>
<cfset arguments.event.addResult("failure") />
<cfset arguments.event.setValue("message","There's a problem with your Dream...") />
</cfif>
<cfset arguments.event.addResult("success") />
<cfset arguments.event.setValue("message","Your Dream has been Saved!") />
<cfelse>
<cfset arguments.event.addResult("failure") />
<cfset arguments.event.setValue("message","There's a problem with your Dream...") />
</cfif>
the value "message" was NOT present in my viewstate!
<cfif retVal is "success">
<cfset arguments.event.setValue("message","Your Dream has been Saved!") />
<cfset arguments.event.addResult("success") />
<cfelse>
<cfset arguments.event.setValue("message","There's a problem with your Dream...") />
<cfset arguments.event.addResult("failure") />
</cfif>
<cfset arguments.event.setValue("message","Your Dream has been Saved!") />
<cfset arguments.event.addResult("success") />
<cfelse>
<cfset arguments.event.setValue("message","There's a problem with your Dream...") />
<cfset arguments.event.addResult("failure") />
</cfif>
the value "message" WAS present in my viewstate!
Apparently, once Modelglue sees that a Result value has been added to the event object, "that's all she wrote, time to evaluate actions and ignore whatever code follows!"Just thought I'd share this in case it saves someone else fifteen minutes.
Subscription Options
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
Re: ModelGlue addResult/setValue Order Matters!
Almost 100% correct :).
Results that have redirect="true" perform the redirect immediately, halting execution of listener functions (sounds weird, but it's necessary: imagine the confusion of two different listeners each request directs. Who wins? The developer sure doesn't, because it'd be a real thrill to debug!).
If redirect isn't specified, the rest of the listener executes, adding values, etc.
Results that have redirect="true" perform the redirect immediately, halting execution of listener functions (sounds weird, but it's necessary: imagine the confusion of two different listeners each request directs. Who wins? The developer sure doesn't, because it'd be a real thrill to debug!).
If redirect isn't specified, the rest of the listener executes, adding values, etc.
Posted by Joe Rinehart on September 21, 2007 at 9:55 AM
Re: ModelGlue addResult/setValue Order Matters!
Thanks for the input, Joe! Just one of those aspects of Modelglue I and others may not have been aware of.
Posted by dougboude on September 21, 2007 at 10:27 AM