Mark Drew (Redux)- cf_etc...

a compendium of railo, cfml, cfeclipse and technology topics

Mark Drew (Redux)- cf_etc...

Adding application variables to ModelGlue.xml

January 8, 2007 ·

Continuing from my previous post, you might also want to add some application variables that you can access in your views easily. To start with, ModelGlue comes with a generic bean that you can use to setup some values without having to create your own beans. You set this in the Coldspring.xml file and in this example, I am going to add some site wide information, such as a copyright value that will go in the footer. <bean id="copyrightConfig" class="ModelGlue.Bean.CommonBeans.SimpleConfig">
      <property name="config">
         <map>
            <entry key="year">
               <value>2007</value>
            </entry>
            <entry key="company">
               <value>Mark Drew Inc. PLC. SA. PTY. Ltd.</value>
            </entry>
            <entry key="link">
               <value>http://www.markdrew.co.uk</value>
            </entry>
            
         </map>
      </property>
   </bean>
What we have done here is set up an object called copyrightConfig with some values in it. The <map> item is actually setting up a structure. Now that we have a bean with our application wide configuration settings, we would like to put them available to all the views via the ViewState object. To do this, in the default controller we add code to the onRequestStart function to load our bean and put it in the ViewState: <cfset stConfig = getModelGlue().getBean("copyrightConfig").getConfig() />
   <cfloop collection="#stConfig#" item="conf">
      <cfset arguments.event.setValue("copyright_" & conf, stConfig[conf])>
   </cfloop>
The first line gets the copyrightConfig bean's config structure (remember those values we added to the <map>?), then we loop through the keys in the structure (adding "copyright_" to the start, just so we know what they are) adding them to the event object. Later on in the view, we have available in the viewState the values as follows: <div id="footer">
      Copyright &amp;copyright; #ViewState.getValue("copyright_year")# <a href="#ViewState.getValue("copyright_link")# ">#ViewState.getValue("copyright_company")# </a>
   </div>
If you don't like adding the "copyright_" to each of the values you could add the configuration structure to the the copyright key: <cfset arguments.event.setValue("copyright", getModelGlue().getBean("copyrightConfig").getConfig())> And then access the items as follows: <cfset stCopyright = ViewState.getValue("copyright")>
   <div id="footer">
      Copyright &amp;copyright; #stCopyright.year# <a href="#stCopyright.link# ">#stCopyright.company# </a>
   </div>
the only thing is that by accessing the values like this, you are taking away the in-built help that the ViewState.getValue() function gives you that if the value doesn't exist, it doesn't barf.

Tags: coldfusion · model-glue

3 responses

  • 1 Lola LB // Sep 22, 2008 at 4:10 PM

    Where is &quot;onRequestStart&quot; function supposed to be located?
  • 2 Lola LB // Sep 22, 2008 at 4:10 PM

    Ah, never mind . . . got it figured out. Temporary brain lapse. :-)
  • 3 Mark Drew // Sep 22, 2008 at 4:10 PM

    Its there in the controller.cfc usually :) If not, you can add one in the controller.