Over a million developers have joined DZone.

ColdBox Event Chaining

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

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.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Published at DZone with permission of John Whish , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}