Where do Coldfusion's implicit events fit in?

28 views
Skip to first unread message

marc

unread,
Jan 14, 2011, 8:22:44 AM1/14/11
to model-glue
Hi,

I have been working a bit with Model-Glue now (Gesture) and realized
there are no implicit events (OnRequestStart, OnRequestEnd,
onSessionStart etc) defined in the MG frameWork. I want some events to
happen on applicationStart, some on sessionStart. I could of course
start coding in Application.cfc but rather keep that code in
controllers/services etc. I remember that in Coldbox these events
could be defined in the controller. Should I assume they just fire and
register them in ModelGlue.xml - so I get something like this:

<controller name="applicationController"
type="application.controller.controller">
<message-listener message="ModelGlue.onApplicationStart"
function="onApplicationStart" " />
<message-listener message="ModelGlue.onSessionStart"
function="onSessionStart/>
<message-listener message="ModelGlue.onRequestStart"
function="onRequestStart/>
<message-listener message="ModelGlue.onRequestEnd"
function="onRequestEnd" />
<message-listener message="ModelGlue.onsessionEnd"
function="onsessionEnd" />
<message-listener message="ModelGlue.onApplicationEnd"
function="onApplicationEnd" " />
</controller>

and define the corresponding events in the controller?

Marc

todd sharp

unread,
Jan 14, 2011, 8:44:48 AM1/14/11
to model...@googlegroups.com
Have you had a look at the MG documentation?

I'd start here:

http://docs.model-glue.com/wiki/HowTos/ApplicationCFCIntegration#Application.cfcIntegrationwithModel-Glue3



--
Model-Glue Sites:
Home Page: http://www.model-glue.com
Documentation: http://docs.model-glue.com
Bug Tracker: http://bugs.model-glue.com
Blog: http://www.model-glue.com/blog

You received this message because you are subscribed to the Google
Groups "model-glue" group.
To post to this group, send email to model...@googlegroups.com
To unsubscribe from this group, send email to
model-glue+...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/model-glue?hl=en



--
Todd Sharp
Sharp Interactive, LLC
http://slidesix.com -- Multimedia Enabled Presentation Sharing
IM:  cfsi...@gmail.com
Blog:  http://cfsilence.com
Twitter: cfsilence | slidesix

marc

unread,
Jan 14, 2011, 9:44:36 AM1/14/11
to model-glue
Oops, overlooked that one. My bad.

Guess I'll have to use application.cfc for the other events -
onRequestStart/onRequestEnd/onApplicationEnd?

Marc

On Jan 14, 2:44 pm, todd sharp <t...@cfsilence.com> wrote:
> Have you had a look at the MG documentation?
>
> I'd start here:
>
> http://docs.model-glue.com/wiki/HowTos/ApplicationCFCIntegration#Appl...
> > model-glue+...@googlegroups.com<model-glue%2Bunsu...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/model-glue?hl=en
>
> --
> Todd Sharp
> Sharp Interactive, LLChttp://slidesix.com-- Multimedia Enabled Presentation Sharing
> IM:  cfsile...@gmail.com

Dan Wilson

unread,
Jan 14, 2011, 9:50:42 AM1/14/11
to model...@googlegroups.com
ModelGlue has built in events for those as well.

Just define an event handler for ModelGlue.onRequestStart ( or any of the others ) and you'll be fine.


DW

Sent from a mobile. Please excuse brevity and typos.

Ezra Parker

unread,
Jan 14, 2011, 6:11:39 PM1/14/11
to model...@googlegroups.com
There are actually two ways you can approach this -- you can either
use an event-handler, as Dan mentioned, or you can add
message-listeners for the messages that are broadcast by the built-in
event handlers.

There are six built-in event-handlers that are commonly used:

modelglue.onApplicationStart
modelglue.onSessionStart
modelglue.onSessionEnd
modelglue.onRequestStart
modelglue.onRequestEnd
modelglue.onQueueComplete

Each of these event-handlers broadcasts a message of the same name
without the "modelglue." prefix, so for example, the
modelglue.onRequestStart event-handler broadcasts an onRequestStart
message.

So if you wish, you can just add a message-listener for the
appropriate message broadcast -- see the page that Todd referenced for
examples of this.

If you prefer, however, you can define the relevant event-handler as
Dan described, and broadcast whatever messages you would like.

The first five events listed should be intuitive enough as to when
they are invoked -- the sixth, onQueueComplete, is fired after all the
views for a request have been queued, and before they are rendered.

Do note that Model-Glue does not have any support for CF's
onApplicationEnd event, so you will need to add this to your app's
Application.cfc file if you wish to use it.

--
Ezra Parker

marc

unread,
Jan 16, 2011, 3:50:58 PM1/16/11
to model-glue
Hi Ezra,

Thanks for the explanation. I am afraid though the onSessionStart
handler doesn't execute when I arrange things like you describe.

Here is what I do:

ModelGlue.xml contains

<controller name="applicationController"
type="application.controller.controller">
<message-listener message="modelglue.onSessionStart"
function="onSessionStart" />
<message-listener message="modelglue.onRequestStart"
function="onRequestStart" />
[more message-listeners]
</controller>

application.controller.controller contains

<cffunction name="onSessionStart" access="public" output="false"
hint="Coldfusion implicit event">
<cfset var localData={}>
<cfset
localData.utilityService=APPLICATION["_modelglue"].getBean("UtilityService")>
<!--- Set flag letting MG know it needs to broadcast onSessionStart
before onRequestStart --->
<cfset request._modelglue.bootstrap.sessionStart = true />
<cfset
session.locale=localData.utilityService.convertAccLanToLocale()>
<cfdump var="#getLocale()#">
<cfabort>
</cffunction>

When I start a new session by deleting domain cookies, the app returns
to the login screen. It should show the <cfdump var="#getLocale()#">
since I request a page without sendinf CFID/CFTOKEN/JSessionId
cookies. I checked Application.cfc and it didn't contain an
onSessionStart handler that might get called instead of the one in the
controller.
So it looks like the onSessionStart() doesn't get executed in
controller.
When I move the onSessionStart() handler from controller/controller to
Application.cfc and I remove domain cookies again, a page refresh
gives me the <cfdump>.

Then I tried with onRequestStart and this didn't work either - until I
changed the message listener from

<message-listener message="modelglue.onRequestStart"
function="onRequestStart" />

to
<message-listener message="onRequestStart"
function="onRequestStart" />

Then I got to the <cfdump> I put in it after a page refresh.

I am not sure if I am missing or overlooking something - just let you
know

I use MG - versionIndicator key says "GESTURE" (didn't find something
like getModelGlue().getVersion() so dumped getModelGlue()) on CF9,0,1

Marc

Matt Quackenbush

unread,
Jan 16, 2011, 3:59:42 PM1/16/11
to model...@googlegroups.com
Marc,

I am pretty certain that this...


<!--- Set flag letting MG know it needs to broadcast onSessionStart
before onRequestStart --->
               <cfset request._modelglue.bootstrap.
sessionStart = true />
... needs to be in your App.cfc's onSessionStart() method.  If you move that to there, then your onSessionStart controller method(s) should work.

HTH

Chris Blackwell

unread,
Jan 16, 2011, 4:24:32 PM1/16/11
to model...@googlegroups.com
If you're setting up listeners in a controller just use onSessionStart (not modelglue.onSessionStart)

<message-listener message="onSessionStart" function="onSessionStart" />

if you want an event-handler to be fired onSessionStart (or any of the other implicit events then use modelglue.onSessionStart

<event-handler name="modelglue.onSessionStart">

Chris


Marc

--
Model-Glue Sites:
Home Page: http://www.model-glue.com
Documentation: http://docs.model-glue.com
Bug Tracker: http://bugs.model-glue.com
Blog: http://www.model-glue.com/blog

You received this message because you are subscribed to the Google
Groups "model-glue" group.
To post to this group, send email to model...@googlegroups.com
To unsubscribe from this group, send email to
model-glue+...@googlegroups.com

marc

unread,
Jan 16, 2011, 4:30:18 PM1/16/11
to model-glue
Ok, that did the trick. I thought I had to remove the onSessionStart()
from Application.cfc. Put it back in and everything works as
advertised.

Thanks

Marc

Dan Wilson

unread,
Jan 16, 2011, 5:08:41 PM1/16/11
to model...@googlegroups.com
Marc,

Feel free to either update the Wiki ( http://docs.model-glue.com ) where you found them lacking, or if you aren't comfortable with that, please email us your suggestions as to how we can make the improvements.


DW
Sent from a mobile. Please excuse brevity and typos.

-----Original Message-----
From: marc <marc.at...@gmail.com>
Sender: model...@googlegroups.com
Date: Sun, 16 Jan 2011 13:30:18
To: model-glue<model...@googlegroups.com>
Reply-To: model...@googlegroups.com
Subject: [Model-Glue] Re: Where do Coldfusion's implicit events fit in?

marc

unread,
Jan 17, 2011, 5:52:02 AM1/17/11
to model-glue
@Chris

I tried this:

===== snip ===
Then I tried with onRequestStart and this didn't work either - until I
changed the message listener from

<message-listener message="modelglue.onRequestStart"
function="onRequestStart" />

to
<message-listener message="onRequestStart"
function="onRequestStart" />

Then I got to the <cfdump> I put in it after a page refresh.

===== snip ===

Which seems to be just the opposite of what you suggest. Unless I
misread I understand from your post that in order to fire on the
implicit Coldfusion generated events (onRequestStart etc) I should use
<event-handler name="modelglue.onSessionStart">.

Btw why would use <message-listener message="onSessionStart"
function="onSessionStart" /> if it's not being triggered by CF's
implicit event - onSessionStart?

Marc

Ezra Parker

unread,
Jan 17, 2011, 5:34:59 PM1/17/11
to model...@googlegroups.com
Marc,

I'm not entirely clear as to where things stand for you at this point
-- you sent a message about 24 hours ago that seemed to state you had
sorted out the onSessionStart issue, but then this latest message
appears to indicate that there is still a problem (specifically, the


"if it's not being triggered by CF's

> implicit event - onSessionStart" portion). Is this working for you?

From this latest message, I think that there is a bit of confusion as
to how CF's application lifecycle events are handled by Model-Glue. I
would recommend going with the simplest approach here, which is to
define a message-listener in your XML config, along with a
corresponding function in your controller.

The message-listener should look like this:

<message-listener message="onSessionStart" function="onSessionStart" />

Or if you prefer, you can omit the "function" attribute, as it is
optional if the function name is the same as the message name:

<message-listener message="onSessionStart" />

Then in your controller, your function should be named "onSessionStart":

<cffunction name="onSessionStart" access="public" output="false">

It is important that you do NOT alter the onSessionStart function in
Application.cfc (the same goes for onSessionEnd) -- it should look
like this:

<cffunction name="onSessionStart" output="false">


<!--- Set flag letting MG know it needs to broadcast onSessionStart
before onRequestStart --->
<cfset request._modelglue.bootstrap.sessionStart = true />

</cffunction>

(In the original version, there is a call to the invokeSessionEvent
function that is commented out -- you do not need this for normal
usage of the onSessionStart event.)

You do NOT need to define a modelglue.onSessionStart event-handler in
order for this to work. As a matter of fact, if you do define this
event-handler, you must also define the onSessionStart message within
it -- otherwise, this message will not be broadcast, as you will be
replacing the internal modelglue.onSessionStart event-handler with
your version, and therefore its message broadcast will not occur.

Finally, I wanted to mention a couple of things regarding the
controller code you posted. First, these lines should not be in your
controller:

<!--- Set flag letting MG know it needs to broadcast onSessionStart
before onRequestStart --->
<cfset request._modelglue.bootstrap.sessionStart = true />

As I mentioned above, this code needs to be in the onSessionStart
function of Application.cfc, and it does serve any purpose in the
onSessionStart function of a controller.

Also, I would recommend using a different approach for accessing
services in controllers -- currently, you have this:

<cfset localData.utilityService=APPLICATION["_modelglue"].getBean("UtilityService")>

In Model-Glue 3, the best practice is to use the "beans" scope for
this task -- please see this wiki page for details:

http://docs.model-glue.com/wiki/HowTos/HowToUseBeanInjection

After adding the UtilityService bean to the controller's XML config or
metadata, you can reference the service like so:

<cfset session.locale = beans.UtilityService.convertAccLanToLocale()>

Although this would be my strong recommendation, if you want to use
the getBean() method instead, then I would definitely suggest using
the getModelGlue() method to retrieve the framework rather than a
direct reference to the application scope:

<cfset localData.utilityService = getModelGlue().getBean("UtilityService")>

Please let me know if any of this is unclear, or merits further explanation.

--
Ezra Parker

Reply all
Reply to author
Forward
0 new messages