Wednesday, April 21, 2010

ColdMVC: Event Listeners

ColdMVC provides your application with several interception points throughout the lifecyle of a request. This is possible thanks to centralized event dispatching from ColdMVC’s EventDispatcher component. In a typical ColdMVC request, the following events will be dispatched:

• requestStart
• actionStart
• preAction
• pre:{controller}Controller
• pre:{controller}Controller.{action}
• action
• post:{controller}Controller:{action}
• post:{controller}Controller
• postAction
• actionEnd
• requestEnd

Any controller within the application can listen for these events by applying metadata to the desired listener method. The events metadata is a comma-separated list of regular expressions, providing quite a bit of flexibility in intercepting. As an example, if you wanted a SecurityController to verify a user is logged in at the beginning of each request, you could have the following code:

component {

* @events requestStart
function verifyLoggedIn() {
if (!session.isLoggedIn) {
redirect({controller="security", action="logout"});


Furthermore, ColdMVC will implicitly invoke certain methods on your request’s controller if they are defined. Before the request’s action is invoked, ColdMVC will invoke the controller’s pre and pre{Action} methods if they exist. Next, ColdMVC will invoke the action for the request, followed by the post{Action} and post methods if they exist. For example, if the current requst is /product/list, ColdMVC will invoke ProductController.pre(), ProductController.preList(), ProductController.list(), ProductController.postList(), and finally

1 comment:

  1. About 2 weeks ago I spent some time cleaning up the event listeners and in doing so I removed the actionStart and actionEnd events in favor of just preAction and postAction, since they're essentially the same. A typical request will now broadcast the following events:

    * requestStart
    * preAction
    * preAction:{controller}Controller
    * preAction:{controller}Controller.{action}
    * postAction:{controller}Controller.{action}
    * postAction:{controller}Controller
    * postAction
    * preLayout
    * preLayout:layoutController
    * preLayout:layoutController.{layout}
    * postLayout:layoutController.{layout}
    * postLayout:layoutController
    * postLayout
    * preView
    * preView:{view}.cfm
    * postView:{view}.cfm
    * postView
    * requestEnd


Note: Only a member of this blog may post a comment.