Aug
13
2007
Model Glue Tips Part 5: Maximize your re-usability with message arguments
Posted by Mark Drew at 2:00 PM
5 comments - Categories:
model-glue
<cffunction name = "getPeople" returnType = "query" access = "public">
<cfargument name="filterby" type="string" required="false" default="">
<cfargument name="filterstring" type="string" required="false" default="">
<cfargument name="orderby" type="string" required="false" default="">
... some sql to do get the People ....
</cffunction>
Initially you would call it as follows:
ModelGlue.xml:
<message name="getPeople"></message>
This would simply get the people, and the controller would return it in the qPeople value (see the code below) in the Viewstate.
Controler Code:
<cfinvoke component="#variables.PeopleService#" method="getPeople" returnVariable="r_qPeople">
</cfinvoke>
<cfset event.setValue("qPeople", r_qPeople)>
Ok, that is pretty basic, but lets say I don't like it being in the qPeople variable, I can change the behavior by adding a little bit of code in the controller, so if you now pass an argument to the message-listener like this:
ModelGlue.xml:
<message name="getPeople">
<argument name="retVariable" value="qMyPeople"/>
</message>
And in the controller code, we remove the hard coded "qPeople" and put in the value of the argument we passed in (with a default of course, don't want our old code to break!) by putting event.getArgument("retVariable", "qPeople") :
<cfinvoke component="#variables.PeopleService#" method="getPeople" returnVariable="r_qPeople">
</cfinvoke>
<cfset event.setValue(event.getArgument("retVariable", "qPeople"), r_qPeople)>
Mow the results would be returned into the qMyPeople variable in the View State.
We can get even cleverer, we could add a filter to our ModelGlue.xml:
<message name="getPeople">
<argument name="retVariable" value="qMyPeople"/>
<argument name="filterby" value="name" />
<argument name="filterstring" value="Mark Drew"/>
</message>
Controller Code:
<cfinvoke component="#variables.PeopleService#" method="getPeople" returnVariable="r_qPeople">
<cfif event.argumentExists("filterby") AND event.argumentExists("filterstring")>
<cfinvokeargument name="filterby" value="#event.getArgument("filterby")#">
<cfinvokeargument name="filterstring" value="#event.getArgument("filterstring")#">
</cfif>
</cfinvoke>
<cfset event.setValue(event.getArgument("retVariable", "qPeople"), r_qPeople)>
Now we are filtering by the string "Mark Drew"!
This is useful only if you want to hard code "Mark Drew" in your ModelGlue.xml file, but you will probably want to pass in a variable through a form or url, so we could change the controller code to say something like:
<cfinvoke component="#variables.PeopleService#" method="getPeople" returnVariable="r_qPeople">
<cfif event.argumentExists("filterby")
AND event.argumentExists("filterstring")
AND event.valueexists(event.getArgument("filterstring"))>
<cfinvokeargument name="filterby" value="#event.getArgument("filterby")#">
<cfinvokeargument name="filterstring" value="#event.getValue(event.getArgument("filterstring"))#">
</cfif>
</cfinvoke>
<cfset event.setValue(event.getArgument("retVariable", "qPeople"), r_qPeople)>
And our Model Glue to:
<message name="getPeople">
<argument name="retVariable" value="qMyPeople"/>
<argument name="filterby" value="name" />
<argument name="filterstring" value="searchfield"/>
</message>
What is going on here? lots of event-ing! , we have changed the cfif statement to include the line event.valueExists(event.getArgument("filterstring")), which gets the the string "searchfield" from our arguments, and then checks to see if we have a form or url variable named "searchfield". If there is one, in the cfinvokeargument, we then go and get that variable and pass it to the function.
Now you are cooking with gas!

Raymond Camden wrote on 09/22/08 11:13 AM
Hey Mark, can you make this tweak:http://www.coldfusionjedi.com/index.cfm/2007/7/26/Important-Update-for-BlogCFC-Users