EventDispatcher Class
The EventDispatcher provides methods for managing prioritized queues of event listeners and dispatching events. All DisplayObject classes dispatch events, as well as some of the utilities like Ticker.
You can either extend this class or mix its methods into an existing prototype or instance by using the EventDispatcher initialize method.
Example
Add EventDispatcher capabilities to the "MyClass" class.
EventDispatcher.initialize(MyClass.prototype);
Add an event (see addEventListener).
instance.addEventListener("eventName", handlerMethod);
function handlerMethod(event) {
console.log(event.target + " Was Clicked");
}
Maintaining proper scope
When using EventDispatcher in a class, you may need to use Function.bind
or another approach to
maintain you method scope. Note that Function.bind is not supported in some older browsers.
instance.addEventListener("click", handleClick.bind(this));
function handleClick(event) {
console.log("Method called in scope: " + this);
}
Please note that currently, EventDispatcher does not support event priority or bubbling. Future versions may add support for one or both of these features.
Constructor
EventDispatcher
()
Item Index
Methods
Properties
Methods
addEventListener
-
type
-
listener
Adds the specified event listener. Note that adding multiple listeners to the same function will result in multiple callbacks getting fired.
Example
displayObject.addEventListener("click", handleClick);
function handleClick(event) {
// Click happened.
}
Parameters:
dispatchEvent
-
eventObj
-
[target]
Dispatches the specified event to all listeners.
Example
// Use a string event
this.dispatchEvent("complete");
// Use an object
var event = {
type: "complete",
foo: "bar"
};
this.dispatchEvent(event);
Parameters:
-
eventObj
Object | StringAn object with a "type" property, or a string type. If a string is used, dispatchEvent will construct a generic event object with the specified type.
-
[target]
Object optionalThe object to use as the target property of the event object. This will default to the dispatching object.
Returns:
hasEventListener
-
type
Indicates whether there is at least one listener for the specified event type.
Parameters:
-
type
StringThe string type of the event.
Returns:
initialize
-
target
Static initializer to mix in EventDispatcher methods.
Parameters:
-
target
ObjectThe target object to inject EventDispatcher methods into. This can be an instance or a prototype.
initialize
()
protected
Initialization method.
removeAllEventListeners
-
[type]
Removes all listeners for the specified type, or all listeners of all types.
Example
// Remove all listeners
displayObject.removeAllEvenListeners();
// Remove all click listeners
displayObject.removeAllEventListeners("click");
Parameters:
-
[type]
String optionalThe string type of the event. If omitted, all listeners for all types will be removed.
removeEventListener
-
type
-
listener
Removes the specified event listener.
Important Note: that you must pass the exact function reference used when the event was added. If a proxy function, or function closure is used as the callback, the proxy/closure reference must be used - a new proxy or closure will not work.
Example
displayObject.removeEventListener("click", handleClick);