MG3: request formats

1 view
Skip to first unread message

Chris Blackwell

unread,
Jul 28, 2008, 4:05:03 PM7/28/08
to model-glue
I'm trying to use a single event to handle updating a record, either
via traditional form submit or via ajax xhr.

this is my event handler

<event-handler name="user.save"
type="model.event.SecuredEventHandler">
<broadcasts>
<message name="updateUser" />
</broadcasts>
<results format="json">
<result name="User.ValidationError" do="json.validationerror"
redirect="true" append="userid" />
</results>
<results format="html">
<result name="User.ValidationError" do="user.edit" redirect="true"
append="userid" />
<result name="User.Saved" do="user.index" redirect="true" />
</results>
</event-handler>

I'm submitting a user form, with requestFormat=json and my controller
is correctly adding a User.ValidationError result, however its running
the result for html and redirecting to user.edit If i remove the
<results format="html"> block then my json result runs fine

I've used the format="json" attribute successfully for broadcasts and
views, but this is the first attempt at using it with results, i'm
hoping it is supported ?

Chris

Joe Rinehart

unread,
Jul 29, 2008, 9:26:17 AM7/29/08
to model...@googlegroups.com
This may be a bug in format-specific result handling, I'll look into
it when I can. If you'd like to help me verify, feel free to open up
EventContext.cfc, jump down to the part where it's handling results,
and if you could look into what's going on when it dequeues results
it'd be handy - feel free to file any findings at http://bugs.model-glue.com/jira

Thanks,

Joe

Chris Blackwell

unread,
Jul 29, 2008, 10:42:56 AM7/29/08
to model-glue
Hi Joe,

Thanks for the reply, this has been driving me a bit crazy :)

I've had a look at EventContext.cfc and i think the issue is with the
addResult() method, and the way it handles results with redirect=true.

The addResult method loops over the eventhandler results for every
format, not just the current requestFormat. So when it finds one that
matches, that has redirect=true it will immediately redirect to the
new event preventing the result for the current requestFormat from
ever being reached.

I've modified my EventContent.addResult() to look like this

<cffunction name="addResult" access="public" hint="Adds a result, by
name, to the result queue.">
<cfargument name="resultName" type="string" hint="The name of the
result (e.g., ""formInvalid"" or the like) to add." />

<cfset var results = getResults() />
<cfset var format = "" />
<cfset var formatList = listappend(getValue("requestFormat", ""),
"cfNullKeyWorkaround") />
<cfset var i = "" />
<cfset var eh = getCurrentEventHandler() />
<cfset var result = "" />

<cfset trace("Message Listener", "A named result
""#arguments.resultName#"" has been added.") />

<cfloop list="#formatList#" index="format">

<cfif structkeyexists(eh.results, format) AND
structKeyExists(eh.results[format], arguments.resultName)>
<cfloop from="1" to="#arrayLen(eh.results[format]
[arguments.resultName])#" index="i">
<cfset result = eh.results[format][arguments.resultName][i] />

<cfif result.redirect>
<cfset this.trace("Result", "Explicit result redirecting to event
""#result.event#""", "<result do=""#result.event#"" redirect=""true"" /
>") />
<cfset forward(eventName:result.event,
preserveState:result.preserveState, addToken:false,
append:result.append, anchor:result.anchor) />
</cfif>
</cfloop>
</cfif>

</cfloop>

<cfset arrayAppend(variables._results, arguments.resultName) />
</cffunction>


I've replaced the outer loop over all formats with a loop over the
current requestFormat, and "cfNullKeyWorkaround" only (which i believe
is for results with no format specified). This seems reasonable to
me, as I can't think why you would want alternate request format
results being fired on the current request.

Hopefully that makes some sense, but feel free to point out the
obvious gotchas that i've missed :)

Cheers, Chris



On Jul 29, 2:26 pm, Joe Rinehart <j...@firemoss.com> wrote:
> This may be a bug in format-specific result handling, I'll look into  
> it when I can.  If you'd like to help me verify, feel free to open up  
> EventContext.cfc, jump down to the part where it's handling results,  
> and if you could look into what's going on when it dequeues results  
> it'd be handy - feel free to file any findings athttp://bugs.model-glue.com/jira

Joe Rinehart

unread,
Jul 30, 2008, 7:50:34 AM7/30/08
to model...@googlegroups.com
Hey Chris,

Thanks for finding that - I'll try to get it into the BER this week!

-Joe

Chris Blackwell

unread,
Jul 30, 2008, 1:38:27 PM7/30/08
to model...@googlegroups.com
No probs Joe,

I'm finding the requestFormat feature to be really helpful.

Cheers, Chris

2008/7/30 Joe Rinehart <j...@firemoss.com>

Chris Blackwell

unread,
Nov 28, 2008, 12:18:20 PM11/28/08
to model...@googlegroups.com
Hi Joe,

Just noticed that this bug still exists in svn
I've attached a diff of rev114 against my working copy in case its helps

Cheers, Chris

2008/7/30 Joe Rinehart <j...@firemoss.com>

EventContext.cfc.diff
Reply all
Reply to author
Forward
0 new messages