As shown in the previous post, there is the ability to pass messages back to a message-listener with added information in the form of arguments. This can come in quite handy and allows you to make your controllers a bit more dynamic and re-usable so that they are useful under different situations.

I shall give an example, which you should have spotted if you read the GenericList documentation, but I shall go into more depth here.Lets say we have a simple query in our service, that gets users, but you can also pass in a name filter, and an order by column. To call this method in the service, you could:

a) Create multiple message-listeners in your controller or b) Create a single clever message-listener that expects different arguments

Solution b sounds a little bit smarter doesn't it?

So, lets prototype this, here is our service function definition, you can imagine that there is some nifty SQL or whatever in there:

... some sql to do get the People ....

Initially you would call it as follows:

ModelGlue.xml: 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:

<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: 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") : <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:

Controller Code:

<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:

<cfset event.setValue(event.getArgument("retVariable", "qPeople"), r_qPeople)>

And our Model Glue to:

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!