I wanted a consistent way to access application configuration settings in Model-Glue without embedding too much implementation into my views and as little as possible in the controller(s).

I found a way to do it that works well for me. I don’t know if it is best practice, but it just might be.

Source of knowledge:
http://www.nabble.com/Global-Application-Name-td7582344.html

I’ll start with a simple configuration bean I added to ColdSpring.xml:


  <bean id="genConfig" class="ModelGlue.Bean.CommonBeans.SimpleConfig">
	<property name="config">
		<map>
			<entry key="mailserver">
				<value>localhost</value>
			</entry>
			<entry key="mailuser">
				<value></value>
			</entry>
			<entry key="mailpwd">
				<value></value>
			</entry>
			<entry key="dateformat">
				<value>d/m/yyyy</value>
			</entry>
			<entry key="timeformat">
				<value>h:mm TT</value>
			</entry>
		</map>
	</property>
  </bean>

If I wanted, I could easily break this up into more beans, e.g. mailConfig and localeConfig. The ModelGlue.Bean.CommonBeans.SimpleConfig component has some simple functions/methods we can use later to access the xml settings.

Next, we add a new function to the main controller component Controller.cfc:

	<cffunction name="setGenConfig" access="public" returnType="void"
			hint="I get the config for this site">
		<cfargument name="genConfig" />

		<cfset variables._genConfig = arguments.genConfig />
	</cffunction> 

The best place to call this new function is in the onQueueComplete function in Controller.cfc because this function is always called after all events have been handled and before the page is rendered.

Add this line to that function:

<cfset arguments.event.setValue("genConfig",variables._genConfig.getConfig()) /> 

Now I can access the configuration data that was injected using ColdSpring within my view:

<cfoutput>
#dateFormat(PostDate, viewState.GetValue("genConfig").dateformat)#
#timeFormat(PostDate, viewState.GetValue("genConfig").timeformat)#
</cfoutput>

From here on, all my views will have access to viewState.GetValue(”genConfig”).config_item_name. Pretty straight forward, right?

Technorati Tags: , , ,


No Responses to “Injecting application configuration settings into ModelGlue using ColdSpring”  

  1. No Comments

Leave a Reply