Skip to content

Working with EventManager

Andrea Campolonghi edited this page Aug 13, 2010 · 8 revisions

Create a CFEM instance

p.Create an instance of CFEM is quite easy cause no configuration is required and anything can setted at runtime.
So simply do:

var application.cfem = createObject('component','com.andreacfm.cfem.EventManager').init();

You are up and running and yes you can have as meny EventManager instances as you like. Remember that any instance is isolated and will not know nothing about others instances events, listeners etc…

Adding Events

Before you can dispatch any event you must register the event to CFEM. By default an event must be registered before you can subscribe listeners to it ( a single exception is done if you decide to subscribe a listener using function metadata. More on this later ).

Registering an event is a matter of passing to CFEM the event name and a custom event class if you want to use a special event type ( read more about Events ).

application.cfem.addEvent(name = "onMyEvent");

or you can also add more events at a time ( recomended cause is quite faster that adding one by one ) like :

var events = [
       {name = 'myevent'},
       {name= 'anotherevent'}
]
//Using the  contructor
var application.cfem = createObject('component','com.andreacfm.cfem.EventManager').init(events = events);
//Or At Runtime
application.cfem.setEvents(events);

Subscribe Listeners

Once that CFEM has registered an event he allows listeners to subscribe to that ( Read more about Listeners ).

A listeners ( as it is for events ) can be subscribed both via the constructor method as later at runtime. A fast example:

// Passing an array of listeners to the constructor.
var listeners = [
	{
		event = 'myevent'
		listener = 'com.myapp.mylistener'
	},
       ....... more ....
]
var application.cfem = createObject('component','com.andreacfm.cfem.EventManager').init(events = [events], listeners = listeners);
//at runtime 
application.em.setListeners(listeners);
//one by one
application.cfem.addEventListener(event = 'myevent', listener = 'com.myapp.mylistener');

more to come