John regularly blogs about ColdFusion, JavaScript and other web technologies and contributes to several FOSS projects. His hobbies include writing in the third person. John is a DZone MVB and is not an employee of DZone and has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

ColdBox Event Chaining

05.20.2011
| 4665 views |
  • submit to reddit

This is probably common knowledge to seasoned ColdBox developers, but I wasn't sure how event chaining worked with runEvent, so I did a simple test.

First off you may wonder why I'd want to run multiple events from one http call, well, I have several things that need to happen overnight, such as updating exchange rates, checking product stock, deleting empty baskets etc. Each of these actions is in a seperate event (so they can be called individually). Being lazy, I didn't want to set up three seperate scheduled tasks.

Here's my test handler:

/**
* I am a ColdBox handler
*/
component
{
  function chaining(event)
  {
    var rc = arguments.event.getCollection();
    runEvent( "legacy.foo" );
    runEvent( "legacy.bar" );
    runEvent( "legacy.foobar" );
  }
  
  function foo(event)
  {
    var prc = arguments.event.getCollection( private=true );
    prc.foo = true;
  }
  
  function bar(event)
  {
    var prc = arguments.event.getCollection( private=true );
    prc.bar = true;
  }
  
  function foobar(event)
  {
    var prc = arguments.event.getCollection( private=true );
    prc.foobar = "pre";
    runEvent( "legacy.nested" );
    prc.foobar = "post";
  }
  
  function nested(event)
  {
    var prc = arguments.event.getCollection( private=true );
    prc.nested = prc.foobar;
  }
}

Nothing out of the ordinary going on here. As you can see I've got a chaining event which executes the other events. I've also added a runevent call inside the foobar method to see what happens. Finally I set up a chaining.cfm view file which simply dumps out the prc struct.

When I run http://localhost/myapp/index.cfm/test/chaining I get the following output.

BAR   true
FOO   true
FOOBAR   post
NESTED   pre 

So ColdBox uses the original events name to find the view template and the application flow returns to the calling method once the runEvent has executed.

References
Published at DZone with permission of John Whish, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: