We have already seen in our previous section all the events that are announced by WireBox, but how do we listen? There are two ways to build WireBox listeners because there are two modes of operations, but the core is the same.
Listeners are simple CFCs that must create methods that match the same name of the event they want to listen to.
If you are running WireBox within a ColdBox application, listeners are Interceptors and you declare them and register them exactly the same way that you do with normal interceptors.
These methods can take up to two parameters depending on your mode of operation (standalone or ColdBox). The one main difference between pure Wirebox listeners and ColdBox interceptors are that the configure method for the standalone WireBox is different.
Argument
Type
Execution Mode
Description
interceptData
struct
standalone-coldbox
The data structure passed in the event
Please note the configure() method in the standalone listener. This is necessary when you are using Wirebox listeners outside of a ColdBox application. The configure() method receives two parameters:
injector : An instance reference to the calling Injector where this listener will be registered with.
properties : A structure of properties that passes through from the configuration file.
As you can see from the examples above, each Listener component can listen to multiple events. Now you might be asking yourself, in what order are these listeners executed in? Well, they are executed in the order they are declared in either the ColdBox configuration file as interceptors or the WireBox configuration file as listeners.
Caution Order is EXTREMELY important for interceptors/listeners. So please make sure you order them in the declaration file.
Argument
Type
Execution Mode
Description
event
coldbox.system.web.context.RequestContext
coldbox
The request context of the running request
interceptData
struct
standalone-coldbox
So let's say that we want to listen on the beforeInjectorShutdown and on the afterInstanceCreation event in our listener.
component{
function configure(injector,properties){
variables.injector = arguments.injector;
variables.properties = arguments.properties;
log = variables.injector.getLogBox().getLogger( this );
}
function beforeInjectorShutdown(interceptData){
// Do my stuff here:
// I can use a log object because ColdBox is cool and injects one for me already.
log.info("DUDE, I am going down!!!");
}
function afterInstanceCreation(interceptData){
var target = arguments.interceptData.target;
var mapping = arguments.interceptData.mapping;
log.info("The object #mapping.getName()# has just been built, performing my awesome AOP processing on it.");
// process awesome AOP on this target
processAwesomeAOP( target );
}
}The data structure passed in the event
buffer
coldbox.system.core.util.RequestBuffer
ColdBox
A request buffer object for producing elegant content in ColdBox applications
rc
struct
coldbox
Reference to the rc scope
prc
struct
coldbox
Reference to the prc scope
component{
function configure(){}
function beforeInjectorShutdown(event, interceptData, buffer, rc, prc ){
var injector = arguments.interceptData.injector;
// Do my stuff here:
// I can use a log object because ColdBox is cool and injects one for me already.
log.info("DUDE, I am going down!!!");
}
function afterInstanceCreation(event, interceptData, buffer, rc, prc ){
var injector = arguments.interceptData.injector;
var target = arguments.interceptData.target;
var mapping = arguments.interceptData.mapping;
log.info("The object #mapping.getName()# has just been built, performing my awesome AOP processing on it.");
// process awesome AOP on this target
processAwesomeAOP( target );
}
}
