Aug 17 2007

Model Glue Tips Part 7: Set myself in your views

Posted by Mark Drew at 6:54 AM
5 comments
- Categories: model-glue

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?

Comments

Raymond Camden

Raymond Camden wrote on 09/22/08 11:13 AM

Yep, this is a good tip. This is two good tips actually. So first, youare right in using the myself VS value to be more 'safe' in the MG framework. The second tip - copying it from the VS into a local var, is also a good one as it saves you a heck of a lot of typing.
Tony Petruzzi

Tony Petruzzi wrote on 09/22/08 11:13 AM

Model-Glue should seriously take a hint from Mach-ii and incorporate a BuildUrl(). It's so nice just having to type BuildURL('my.event').
Rob Munn

Rob Munn wrote on 09/22/08 11:13 AM

Mark,

Nice tip. I also like the practice of coding events into the ModelGlue.xml file, e.g. :

&lt;event-handler name=&quot;cms.showEditObject&quot; access=&quot;public&quot;&gt;
   &lt;broadcasts &gt;
      &lt;message name=&quot;needObject&quot;/&gt;
   &lt;/broadcasts&gt;
   &lt;results /&gt;
   &lt;views&gt;
      &lt;include name=&quot;page&quot; template=&quot;cms/dspShowEditObject.cfm&quot;&gt;
         &lt;value name=&quot;xe.showBinaryObject&quot; value=&quot;showBinaryObject&quot;&gt;&lt;/value&gt;
         &lt;value name=&quot;xe.saveObject&quot; value=&quot;saveObject&quot;&gt;&lt;/value&gt;
         &lt;value name=&quot;xe.deleteObject&quot; value=&quot;deleteObject&quot;&gt;&lt;/value&gt;      
      &lt;/include&gt;
   &lt;/views&gt;
&lt;/event-handler&gt;

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:

&lt;cfset event_path = &quot;&quot; /&gt;
&lt;cfif listlen(viewstate.getValue(&quot;event&quot;), &quot;.&quot;) eq 2&gt;
   &lt;!--- get the first part of the event to make sure we set the exit events correctly ---&gt;
   &lt;cfset event_path = getToken(viewstate.getValue(&quot;event&quot;),1,&quot;.&quot;) &amp; &quot;.&quot; /&gt;
&lt;/cfif&gt;

&lt;cfscript&gt;
   function setPath(basePath,eventPath,varValue){
      if(listLen(varValue,&quot;.&quot;) eq 2)
         return basePath &amp; varValue;
      if(listLen(varValue,&quot;.&quot;) eq 1)
         return basePath &amp; eventPath &amp; varValue;
   }
&lt;/cfscript&gt;

Then I set the events like so:

&lt;cfset submit = setPath(viewstate.getValue(&quot;myself&quot;),event_path,viewState.getValue(&quot;xe.saveObject&quot;))/&gt;
&lt;cfset deleteObject = setPath(viewstate.getValue(&quot;myself&quot;),event_path,viewState.getValue(&quot;xe.deleteObject&quot;))/&gt;
&lt;cfset showBinaryObject = setPath(viewstate.getValue(&quot;myself&quot;),event_path,viewState.getValue(&quot;xe.showBinaryObject&quot;))/&gt;

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:

&lt;event-handler name=&quot;admin.showEditObject&quot;&gt;
   &lt;broadcasts&gt;
   &lt;/broadcasts&gt;
   &lt;results&gt;
      &lt;result do=&quot;cms.showEditObject&quot;&gt;&lt;/result&gt;
      &lt;result do=&quot;layout.getAdminLayout&quot;&gt;&lt;/result&gt;
   &lt;/results&gt;
   &lt;views/&gt;
&lt;/event-handler&gt;   

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.
Sean Corfield

Sean Corfield wrote on 09/22/08 11:13 AM

The self / myself thing comes from Fusebox (as does the concept of eXit Events - eXit FuseActions in Fusebox) and they're both good techniques for keeping the view abstracted from the actual URLs which in turn makes it easier to rewrite an application when the control flow needs to change.

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=&quot;#buildUrl('my.event')#&quot; over href=&quot;#myself##xfa.next#&quot; (where next is an XFA that resolves to my.event).
Jeff Gladnick

Jeff Gladnick wrote on 09/22/08 11:14 AM

This is good, but I need something better: Is there a way to set this so its avaliable on EVERY view somehow?

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 &quot;body&quot; 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?

Write your comment



(it will not be displayed)







Search Archives

Contribute


Twitter Updates

Categories

coldfusion cfeclipse personal apple cfconferences webdev model-glue ukcfug blogging cfunited coldspring presentations adobe scotch on the rocks cfobjective lost reactor jobs flex eclipse ajax model-glue frameworks max reference google podcasting fusebox ask md spam railo eventvalidation donations tattoo snipex podcast music microsoft fusiondebug flying blue dragon air wishlist tips rant open bluedragon java graphic novels funny eventguard derby blog aptana apple xml webservices transfer tashr subversion spry regex quack pownce ooops mvc metrics jquery itunes hosting groovy gadgets flash firefox doh! directing development coldfusioncamp codeshare cfug cfonwheels cfdevcon bolt apollo

Monthly Archives