Over a million developers have joined DZone.
Silver Partner

ColdBox Event Chaining

· Java Zone

The Java Zone is brought to you by Stormpath—offering a pre-built identity API for developers. Easily build powerful user management, authentication, and authorization into your web and mobile applications. Check out this tutorial to build a simple web app with Spring Boot and Spring Security in 15 minutes.

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 by Stormpath—offering a pre-built, streamlined user management API for building web and mobile applications. Learn how token authentication with JWTs (JSON Web Tokens) helps secure websites and avoids the pitfalls of using cookies and sessions.

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 }}