Aliaspooryorik
ColdFusion ORM Book

ColdBox event chaining

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.


No comments

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts