So you want to create a ModelGlue:Unity application? ( Part 5 )
Previously in this series, we installed the ModelGlue:Unity framework and the ColdSpring framework. We used the ModelGlueApplicationTemplate as the skeleton, added our basic flow and navigation. We can save and list contacts and our validation is working nicely,
In this section of our Contact-O-Matic, we will pull out the contact types values from coldspring and used them to add defined categories to our contacts. In a previous segment, we added the Contact Types as a 'map' in ColdSpring, now we will pull out the values and add them to our form.
Inside the *ContactManagerMG/Config/ColdSpring.xml file the bean definition for ContactService.cfc looks like this:
2 <property name="ContactTypes">
4 <entry key="Friend"><value>Friend</value></entry>
5 <entry key="Co-Worker"><value>Co-Worker</value></entry>
6 <entry key="Enemy"><value>Enemy</value></entry>
9 <property name="ContactList">
In order to provide a proper separation in our code, we add a new 'message-listener' tag with the message attribute of 'needContactTypes' and the function attribute of 'getContactTypes'.
Then in our 'contact.view' event-handler, add a new message tag inside broadcasts tag. Your 'contact.view' event-handler should look as follows:
3 <message name="needContactForm" />
4 <message name="needContactTypes" />
7 <include name="body" template="frmContact.cfm">
8 <value name="whichMenuIsCurrent" value="contact.view" />
12 <result do="view.template" />
Note: If copying this code directly, you must change the & lt; to a proper <
Next, in our *ContactManagerMG.controller.controller.cfc, we add a new function called 'getContactTypes'. This function will pull out the 'ContactTypes' map, or struct, from our ContactService and place them in the event.
2 <cfargument name="event" type="any" />
3 <cfset var ContactService = getModelGlue().getBean( "ContactService") />
4 <cfset arguments.event.setValue( "ContactTypes", ContactService.getContactTypes() ) />
Finally, we open our form, located at *ContactManagerMG/views/frmContact.cfm. Pull out the ContactTypes from the event and loop through to create a select box of values. Your completed form should look like this:
2<cfset ContactFormBean = viewstate.getValue("ContactFormBean") />
3<cfset ErrorStruct = viewstate.getValue("ErrorStruct", structNew()) />
4<cfset ContactTypes = viewstate.getValue("ContactTypes") />
8 <cfif structCount( ErrorStruct ) GT 0 >
9 <cfloop collection="#ErrorStruct#" item="errorType">
10 <li>#ErrorStruct[ errorType ]#</li>
14<form id="ContactForm" action="#myself#SubmitContactForm" method="post">
15 <input type="hidden" name="contactID" value="#ContactFormBean.getContactID()#" />
16Name: <input type="text" name="ContactName" value="#ContactFormBean.getContactName()#"><br />
17Type: <select name="ContactType">
18 <cfloop collection="#ContactTypes#" item="thisType">
19 <option value="#thisType#" <cfif ContactFormBean.getContactType() IS thisType>selected</cfif>>#ContactTypes[thisType]#</option>
22 <br />
23 <input type="submit" name="submit" value="Submit" />
Once you reset your application and click the 'contact' tab, you should have a functional select box in your form.
Proper application design would most likely not include storing the data for your select boxes inside coldspring. After all, that is what a database is for. The purpose of this exercise is to show how to retrieve data from ColdSpring to use in your applications.
A practical use of ColdSpring would be to define an email service inside ColdSpring to operate in two modes, production and development. In production mode, the email service would send emails to each email it was passed. In development mode, the email service would send an email to a developer email address. Thus, changing from development mode to production mode is as easy as changing a line in your ColdSpring.xml file.
If you have ever accidently spammed your users whilst testing application email functionality, you can appreciate the benefits ColdSpring offers.
The fruits of our labor
<blockquote><i>"If you have ever accidently spammed your users whilst testing application email functionality, you can appreciate the benefits ColdSpring offers."</i></blockquote>
LOL... that is to perfect.