Class com.sekati.events.Dispatcher

Implemented Interfaces

IDispatchable

Description

A centralized EventDispatcher to decouple event listeners & dispatchers from direct addressing.

 Usage:
 var receiveObj:Object = {_name:"receiveObj"};
 // define a generic method for the reciever object to use
 function onEventReceived (event:Object):Void {
 var str:String = (this._name + " received Event = { type:" + event.type + ", target:" + event.target + ", data: {");
 for (var i in event.data) { str += " " + i + ":" + event.data[i] + ","; }
 trace (str+" }};");
 }
 // add the method to our reciever object as 'testEvent'
 receiveObj.testEvent = onEventReceived;
 // add reciever object as an event listener for "testEvent"
 Dispatcher.getInstance().addEventListener ("testEvent",receiveObj);
 // define sender and data objects (optional)
 var senderObj:Object = this;
 var dataObj:Object = {message:"hello", someNumber:42};
 // Dispatch the event to all 'testEvent' EventListeners
 Dispatcher.getInstance().dispatchEvent (new Event ("testEvent", senderObj, dataObj));
 
Some excellent explanations of the AS2/3 event models and broadcasters vs dispatchers

See Also

Field Index

$

Method Index

addEventListener(), broadcastEvent(), bubbleEvent(), destroy(), dispatchEvent(), getInstance(), removeEventListener()

Inherited from CoreObject

Field Detail

$

static public $:Dispatcher [Read Only]
shorthand singleton accessor getter

Method Detail

getInstance

static public function getInstance():Dispatcher

Singleton Accessor

Return

Dispatcher

addEventListener

public function addEventListener(event:String, handler:Object):Void

Add the event listener to the centralized event manager

Parameters

event(String) the name of the event ("click", "change", etc)
handler(Object) the function or object that should be called

Return

Void

Specified By

addEventListener() in com.sekati.events.IDispatchable

removeEventListener

public function removeEventListener(event:String, handler:Object):Void

Remove the event listener from the centralized event manager

Parameters

event(String) the name of the event ("click", "change", etc)
handler(Object) the function or object that should be called

Return

Void

Specified By

removeEventListener() in com.sekati.events.IDispatchable

dispatchEvent

public function dispatchEvent(e:Event):Void

Dispatch the event to all listeners via the centralized event manager

Parameters

e(Event) an Event or one of its subclasses describing the event

Return

Void

 Usage:
 Dispatcher.getInstance().dispatchEvent(new Event("myEvent", this, {foo:true, bar:false}));
 

Specified By

dispatchEvent() in com.sekati.events.IDispatchable

bubbleEvent

public function bubbleEvent(e:Event):Void

Bubbles event up the chain. The target property is added on the route and then replaced by the new target.

Parameters

e(Event)

Return

Void

Specified By

bubbleEvent() in com.sekati.events.IDispatchable

broadcastEvent

public function broadcastEvent(_type:String, _target:Object, _data:Object):Void

Wrapper to dispatchEvent: creates an Event object and dispatchs it to all event listeners

 Usage:
 Dispatcher.getInstance().broadcastEvent("myEvent",targetObject, {param0:value0, param1:value1, paramn:valuen});
 

Parameters

_type(String) type of event
_target(Object) he object that dispatched this event. There is a known bug with this property: It always returns as '/'. This may be a flaw in EventDispatcher; if you need to pass the event source use dispatchEvent.
_data(Object) a transport object for any needed data

Return

Void

Specified By

broadcastEvent() in com.sekati.events.IDispatchable

destroy

public function destroy():Void

Destroy singleton instance.

Return

Void

Specified By

destroy() in com.sekati.core.CoreInterface

Overrides

destroy() in com.sekati.core.CoreObject