Model Glue Tips Part 7: Set myself in your views
This is a quick tip for today. In all views you will probably be linking to other events, now of course you could write something like:
<a href="#CGI.SCRIPT_NAME#?event=my.event">My Event </a>
But what happens if you change the event key to something else (you can do that in the ModelGlue configuration section in the ColdSpring.xml file, see the property for "eventValue") such as "go" or "action"? You would have to change every view.
A simple way round this is to put at the top of each view the following:
<cfset myself = ViewState.getValue('myself')>
And then change your links to:
<a href="#myself#my.event">My Event </a>
What do you think?
Tags: model-glue
7 responses so far ↓
1 Raymond Camden // Sep 22, 2008 at 4:13 PM
2 Tony Petruzzi // Sep 22, 2008 at 4:13 PM
3 Rob Munn // Sep 22, 2008 at 4:13 PM
Nice tip. I also like the practice of coding events into the ModelGlue.xml file, e.g. :
<event-handler name="cms.showEditObject" access="public">
<broadcasts >
<message name="needObject"/>
</broadcasts>
<results />
<views>
<include name="page" template="cms/dspShowEditObject.cfm">
<value name="xe.showBinaryObject" value="showBinaryObject"></value>
<value name="xe.saveObject" value="saveObject"></value>
<value name="xe.deleteObject" value="deleteObject"></value>
</include>
</views>
</event-handler>
I have also gotten into the habit of using two part event names, like cms.showEditObject, in ModelGlue.xml. Then I leave the first part of the event off of the exit event variables (xe.), and dynamically set the first part in my views using a utility function:
<cfset event_path = "" />
<cfif listlen(viewstate.getValue("event"), ".") eq 2>
<!--- get the first part of the event to make sure we set the exit events correctly --->
<cfset event_path = getToken(viewstate.getValue("event"),1,".") & "." />
</cfif>
<cfscript>
function setPath(basePath,eventPath,varValue){
if(listLen(varValue,".") eq 2)
return basePath & varValue;
if(listLen(varValue,".") eq 1)
return basePath & eventPath & varValue;
}
</cfscript>
Then I set the events like so:
<cfset submit = setPath(viewstate.getValue("myself"),event_path,viewState.getValue("xe.saveObject"))/>
<cfset deleteObject = setPath(viewstate.getValue("myself"),event_path,viewState.getValue("xe.deleteObject"))/>
<cfset showBinaryObject = setPath(viewstate.getValue("myself"),event_path,viewState.getValue("xe.showBinaryObject"))/>
In effect, I have created atomic events that I can compose as needed in my application. My cms.showEditObject event could be called through an admin interface, or it could be called inline in the application, each through a separate event, like so:
<event-handler name="admin.showEditObject">
<broadcasts>
</broadcasts>
<results>
<result do="cms.showEditObject"></result>
<result do="layout.getAdminLayout"></result>
</results>
<views/>
</event-handler>
In this separate event, I can include other business logic as dictated by the needs of the application. It ends up being a little bit verbose in the XML, but the trade-off in granularity of control is worth it to me.
4 Sean Corfield // Sep 22, 2008 at 4:13 PM
As for buildUrl(), I'm in two minds about that. Fusebox tackles the problem by allowing XFAs to be constructed in the control layer and automatically handles SES URL generation. Overall, I'm not sure that I prefer href="#buildUrl('my.event')#" over href="#myself##xfa.next#" (where next is an XFA that resolves to my.event).
5 Jeff Gladnick // Sep 22, 2008 at 4:14 PM
I tried doing this in my dspTemplate.cfm file that creates the header/footer, but unfortunately in this case (although fortunately in other cases) that doesnt work because model glue will load the "body" view BEFORE dspTemplate.
Is there a way to globally define a viewstate variable, like i used to do with a quick cfset in application.cfm?
6 Nolan Erck // Apr 8, 2009 at 5:23 AM
Great set of tips! I've been collecting some Model Glue references for reference lately; these will definitely be added to the list (and linked from my blog). :)
Best,
Nolan
7 gucci outlet // Jul 3, 2010 at 6:30 PM
Wang: everybody in the http://www.gucci-zone.com/ gucci outlet swimming pool.
The lifeguard: yes! Sir, but only if you stand on the board in the urine...
Leave a Comment