Model-Glue 3 - Example of Custom Event Types

This post is more than 2 years old.

Now that Model-Glue 3 has been announced and available for folks to play with, it's time to start working up some demos so folks can see stuff in action. Joe has included a few demos in the zip, but if I don't play with it myself, I don't learn.

Before I go any further, and yes, I will be repeating this warning a lot, keep in mind the following two points:

  1. Model-Glue 3 is in Alpha. Everything I talk about here may stop working tomorrow. If it does, I'm going to hunt Joe down and make him rewrite Model-Glue in Pascal.
  2. Like every new feature, there are "good" ways of using it and "not so good" ways of using it. This is the first time I've done this feature. It may be a stupid example. I may look back in a month and ask - what was I drinking.

So with the above in mind, let's dig in.

First off - let's talk about what Custom Event Types (CETs) are. Those of us who develop in Model-Glue are used to the repeated XML we have to use for things like layout and security checks. Consider:

<event-handler name="page.about"> <broadcasts /> <views> <include name="body" template="main.about.cfm" /> </views> <results> <result do="view.template" /> </results> </event-handler> <event-handler name="page.category"> <broadcasts> <message name="GetProjectCategory" /> <message name="GetProjectsForCategory" /> </broadcasts> <views> <include name="body" template="main.category.cfm" /> </views> <results> <result do="view.template" /> </results> </event-handler>

These two simple events both have an unnamed event to run a template view. As you can imagine, my application has this repeated quite a bit. CETs allow us a simple way to get around it. Let's start with the XML changes you have to make.

MG3 supports a type argument to the event-handler tag. Here is a simple example:

<event-handler name="page.index" type="">

The value is the full path to the CFC we will create to define our CET. Note that the value is passed to createObject, so use whatever paths you would normally use.

Ok, so the CFC. As you can see I used an events folder above. This was based on Joe's sample apps and as it made sense, I'm going to pretend it was all my idea and take credit for it.

Your CFC will begin by extending the core EventHandler from Model-Glue. Here is an empty CET:

<cfcomponent extends="ModelGlue.gesture.eventhandler.EventHandler">


It's empty - but boy does it run fast. So out of the box there are 2 built-in events that you potentially will want to use:

  • beforeConfiguration: This is run while the event is starting up. This is where you would add broadcasts. So for a security CET, you would broadcast a message to check authorization/authentication.
  • afterCongifuration: This is run when the event is ending. This is where you would add results. Note that CETs support named and unnamed results. Now to be clear - you can also broadcast stuff as well. But this would run after other broadcasts so if you need to broadcast something that later messages need, you want to use beforeConfiguration.

So first off - those CFC names are a bit oddly named in my book. I just bugged Joe about this. To me, "configuration" implies one time while in reality, these guys are always run, much like onRequestStart/End. This confused me so I'm just sharing it if other folks get confused by it as well. (FYI, Joe is IMing right now, so don't be surprised if it changes. Actually, it's done. It may not be in the zip yet, but it is changing to beforeEvent and afterEvent.)

The second thing that confused me was that I couldn't figure out how to get access to the Event object. After speaking with Joe, I was able to wrap my head around the idea that this CFC code is basically replacing XML. You aren't going to be doing Event checks here - just basically just adding to the XML (via CF) that would have been hard coded in the event normally.

So - so once I got things working, I needed something to code. I wanted to build upon the article I wrote a few days ago concerning multiple templates. The idea there was - how do I support different templates. The answer I used there was to simply have multiple results. Here is sample XML I used from that entry:

<event-handler name="page.index"> <broadcasts /> <results> <result name="print" do="view.printtemplate" /> <result do="view.template" /> </results> <views> <include name="body" template="dspIndex.cfm" /> </views> </event-handler>

<event-handler name="view.template"> <broadcasts /> <results /> <views> <include name="template" template="dspTemplate.cfm" /> </views> </event-handler>

<event-handler name="view.printtemplate"> <broadcasts /> <results /> <views> <include name="template" template="dspPrintTemplate.cfm" /> </views> </event-handler>

This turns out to be very easy in CET. Here is the code I used for afterConfiguration:

<cffunction name="afterConfiguration" access="public" returntype="void" output="false" hint="Called after configuring the event handler. Subclasses can use this to add messages, results, or views after they're added by something like a ModelGlue XML file."> <cfset var result = createObject("component", "ModelGlue.gesture.eventhandler.Result") /> <cfset var secondresult = createObject("component", "ModelGlue.gesture.eventhandler.Result") />
&lt;cfset super.afterConfiguration() /&gt;

&lt;cfset result.event = "template.main" /&gt;
&lt;cfset addResult(result) /&gt;

&lt;cfset secondresult.event="template.printmain"&gt;
&lt;cfset addResult(secondresult)&gt;


I create two results. The first is unnamed and represents my default template. The second is named printformat. To use this new event type, I first specify it in my XML:

<event-handler name="page.index" type=""> <broadcasts /> <results /> <views> <include name="body" template="pages/index.cfm" /> </views> </event-handler>

And I'm done. I made it so that in my application, if "print" exists in the Event (url or form obviously), the printformat result is added. I did this via onRequestEnd in my main controller:

<cffunction name="onRequestEnd" access="public" output="false" returnType="void"> <cfargument name="event" type="any">
&lt;cfif arguments.event.valueExists("print")&gt;
	&lt;cfset arguments.event.addResult("printformat")&gt;


So - this would have been the end of the story. Unfortunately, it didn't work. I bugged Joe again, and he came up with the answer. Unfortunately, the reason it doesn't work is rather low level. The simplest explanation is that onRequestEnd (an automatic broadcast in MG) is actually consdiered another event: modelglue.onRequestEnd.

Joe came up with a solution that isn't quite as short as my previous example, but works ok. Now what we do is simply broadcast a message that will do the same check as my onRequestEnd code above. Take a look at the new version on afterConfiguration:

<cffunction name="afterConfiguration" access="public" returntype="void" output="false" hint="Called after configuring the event handler. Subclasses can use this to add messages, results, or views after they're added by something like a ModelGlue XML file."> <cfset var result = createObject("component", "ModelGlue.gesture.eventhandler.Result") /> <cfset var secondresult = createObject("component", "ModelGlue.gesture.eventhandler.Result") /> <cfset var message = createObject("component", "ModelGlue.gesture.eventhandler.Message") />
&lt;cfset super.afterConfiguration() /&gt;

&lt;cfset = "checkFormat" /&gt;
&lt;cfset addMessage(message) /&gt;

&lt;cfset result.event = "template.main" /&gt;
&lt;cfset addResult(result) /&gt;

&lt;cfset secondresult.event="template.printmain"&gt;
&lt;cfset addResult(secondresult)&gt;


As you can see, I now broadcast checkFormat. I won't show the code for that as it's the exact same as the onRequestEnd above.

Alright - so hopefully that wasn't too confusing! I've attached a zip of my application. Please note that Joe will be updating the MG3 alpha zip fairly shortly so this code may not work right this very second.

Download attached file.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by zac spitzer posted on 5/10/2008 at 8:33 AM

Do you think addResult and addMessage would be improved if they took multiple arguments than passing in a struct?

the code is looking quite verbose for a simple example.

using arguments would reduce the line count to 4 lines from the current 12


Comment 2 by Sean Corfield posted on 5/10/2008 at 8:34 AM

This is the first real explanation I've seen of CETs. I liked the concept a lot when Joe explained it but now I see the implementation, I think it's horrible. It really would be much nicer for CETs to be specified as XML instead of having to dynamically run code on every event. I'll talk to Joe about this.

Comment 3 by Raymond Camden posted on 5/10/2008 at 5:29 PM

@SC - While I don't think it is horrible, I've also told Joe he really needs to looking at a way to do CETs in XML.

Comment 4 by Shimju David posted on 5/11/2008 at 11:01 AM

Frankly speaking like Sean said, concept is good, implementation looks horrible and I would rather stick with our existing way than using this new feature. Even though few repeated lines of code will happen, existing way is better when considering readability of code and simplicity in implementation. Also I hope when MG3 lauches it will have some simple way of Custom Event Types implementation.

Comment 5 by Raymond Camden posted on 5/11/2008 at 5:04 PM

I know Joe is looking at this thread. I've sent him some ideas on this as well, so we shall see. If it didn't change for MG3, I'd still be happy with the feature, but it's still early on in the development.