Mark Drew (Redux)- cf_etc...

a compendium of railo, cfml, cfeclipse and technology topics

Mark Drew (Redux)- cf_etc...

Model Glue Tips Part 7: Set myself in your views

August 17, 2007 · 7 Comments

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

    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.
  • 2 Tony Petruzzi // Sep 22, 2008 at 4:13 PM

    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').
  • 3 Rob Munn // Sep 22, 2008 at 4:13 PM

    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.
  • 4 Sean Corfield // Sep 22, 2008 at 4:13 PM

    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).
  • 5 Jeff Gladnick // Sep 22, 2008 at 4:14 PM

    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?
  • 6 Nolan Erck // Apr 8, 2009 at 5:23 AM

    Hi Mark,

    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

    A lifeguard to visitors to protest: I notice you have three days, you cannot in urine wang swimming pool.
    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