Chaining ModelGlue Generic Commit Events
I have really been loving the generic ORM stuff that is part of ModelGlue 2.0. I had been doing way to much work to stuff the objects we needed into the viewstate. Now I have been able to remove countless listeners and broadcasts I was using to get various objects when needed.
Now a generic read takes care of that.
The basic functionality of the generic events are documented at http://docs.model-glue.com so I wont get into how to use them, but one thing that isn't documented is how to handle updates to multiple unrelated objects in the same form post( event-handler)
Here is how I am doing it, It works like a charm for quick prototyping,as far as I am concerned.
Lets say I have 2 objects, Cinders and Ashes. I am collecting this data on the same form and I need to handle the validation and update the results. I need two generic commit message broadcasts
<message name="modelglue.GenericCommit">
<argument name="object" value="Cinder" />
<argument name="CinderId" value="Cinder" />
</message>and
<message name="modelglue.GenericCommit">
<argument name="object" value="Ashes" />
<argument name="AshesId" value="Ashes" />
</message>My form lives at an event called "CindersAndAshes.form" and submits to an event called "CindersAndAshes.commit"
Placing both broadcasts in this event only caused one of the commits. what I did was to chain two events together:
CindersAndAshes.commit1
<event-handler name="CindersAndAshes.commit1">
<broadcasts>
<message name="ModelGlue.genericCommit">
<argument name="criteria" value="CindersId" />
<argument name="object" value="Cinders" />
</message>
</broadcasts>
<results>
<result name="commit" do="CindersAndAshes.commit2" redirect="true" append="" preserveState="true" />
<result name="validationError" do="CindersAndAshes.form" redirect="false" append="" preserveState="false" />
</results>
</event-handler>CindersAndAshes.commit2
<event-handler name="CindersAndAshes.commit2">
<broadcasts>
<message name="modelglue.GenericCommit">
<argument name="object" value="Ashes" />
<argument name="Criteria" value="AshesId" />
</message>
</broadcasts>
<results>
<result name="commit" do="CindersAndAshes.commit2" redirect="true" append="" preserveState="false" />
<result name="validationError" do="CindersAndAshes.form" redirect="false" append="" preserveState="false" />
</results>
</event-handler>basically a generic commit happens on "Cinders" first then "Ashes", if either fails, the user is redirected to the form and a validation array is placed in the viewstate.
The drawback to doing it this way is that if the first commit fails, the user only immediatly finds out about one set of validation errors which may be corrected. So this is a great technique for prototyping and rapid first passes at functionality I plan on trying out Sean Corfield's concurrency library to try to hit two events and aggregate the validation array's. I will post code If I am able to succeed at that task.
delicious |
digg |
reddit |
technorati
