EventDispatcher Class
EventDispatcher provides methods for managing queues of event listeners and dispatching events.
You can either extend EventDispatcher or mix its methods into an existing prototype or instance by using the EventDispatcher initialize method.
Together with the CreateJS Event class, EventDispatcher provides an extended event model that is based on the DOM Level 2 event model, including addEventListener, removeEventListener, and dispatchEvent. It supports bubbling / capture, preventDefault, stopPropagation, stopImmediatePropagation, and handleEvent.
EventDispatcher also exposes a on method, which makes it easier to create scoped listeners, listeners that only run once, and listeners with associated arbitrary data. The off method is merely an alias to removeEventListener.
Another addition to the DOM Level 2 model is the removeAllEventListeners method, which can be used to listeners for all events, or listeners for a specific event. The Event object also includes a remove method which removes the active listener.
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
Scope (ie. "this") can be be a challenge with events. Using the on
method to subscribe to events simplifies this.
instance.addEventListener("click", function(event) {
console.log(instance == this); // false, scope is ambiguous.
});
instance.on("click", function(event) {
console.log(instance == this); // true, "on" uses dispatcher scope by default.
});
If you want to use addEventListener instead, you may want to use function.bind() or a similar proxy to manage scope.
Browser support The event model in CreateJS can be used separately from the suite in any project, however the inheritance model requires modern browsers (IE9+).
Constructor
EventDispatcher
()
Item Index
Methods
Properties
Methods
_dispatchEvent
-
eventObj
-
eventPhase
addEventListener
-
type
-
listener
-
[useCapture]
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:
-
type
StringThe string type of the event.
-
listener
Function | ObjectAn object with a handleEvent method, or a function that will be called when the event is dispatched.
-
[useCapture]
Boolean optionalFor events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
dispatchEvent
-
eventObj
-
[bubbles]
-
[cancelable]
Dispatches the specified event to all listeners.
Example
// Use a string event
this.dispatchEvent("complete");
// Use an Event instance
var event = new createjs.Event("progress");
this.dispatchEvent(event);
Parameters:
-
eventObj
Object | String | EventAn object with a "type" property, or a string type. While a generic object will work, it is recommended to use a CreateJS Event instance. If a string is used, dispatchEvent will construct an Event instance if necessary with the specified type. This latter approach can be used to avoid event object instantiation for non-bubbling events that may not have any listeners.
-
[bubbles]
Boolean optionalSpecifies the
bubbles
value when a string was passed to eventObj. -
[cancelable]
Boolean optionalSpecifies the
cancelable
value when a string was passed to eventObj.
Returns:
Returns false if preventDefault()
was called on a cancelable event, true otherwise.
hasEventListener
-
type
Indicates whether there is at least one listener for the specified event type.
Parameters:
-
type
StringThe string type of the event.
Returns:
Returns true if there is at least one listener for the specified event.
initialize
-
target
Static initializer to mix EventDispatcher methods into a target object or prototype.
EventDispatcher.initialize(MyClass.prototype); // add to the prototype of the class
EventDispatcher.initialize(myObject); // add to a specific instance
Parameters:
-
target
ObjectThe target object to inject EventDispatcher methods into. This can be an instance or a prototype.
off
-
type
-
listener
-
[useCapture]
A shortcut to the removeEventListener method, with the same parameters and return value. This is a companion to the .on method.
IMPORTANT: To remove a listener added with on
, you must pass in the returned wrapper function as the listener. See
on for an example.
on
-
type
-
listener
-
[scope]
-
[once=false]
-
[data]
-
[useCapture=false]
A shortcut method for using addEventListener that makes it easier to specify an execution scope, have a listener only run once, associate arbitrary data with the listener, and remove the listener.
This method works by creating an anonymous wrapper function and subscribing it with addEventListener.
The wrapper function is returned for use with removeEventListener
(or off
).
IMPORTANT: To remove a listener added with on
, you must pass in the returned wrapper function as the listener, or use
remove. Likewise, each time you call on
a NEW wrapper function is subscribed, so multiple calls
to on
with the same params will create multiple listeners.
Example
var listener = myBtn.on("click", handleClick, null, false, {count:3});
function handleClick(evt, data) {
data.count -= 1;
console.log(this == myBtn); // true - scope defaults to the dispatcher
if (data.count == 0) {
alert("clicked 3 times!");
myBtn.off("click", listener);
// alternately: evt.remove();
}
}
Parameters:
-
type
StringThe string type of the event.
-
listener
Function | ObjectAn object with a handleEvent method, or a function that will be called when the event is dispatched.
-
[scope]
Object optionalThe scope to execute the listener in. Defaults to the dispatcher/currentTarget for function listeners, and to the listener itself for object listeners (ie. using handleEvent).
-
[once=false]
Boolean optionalIf true, the listener will remove itself after the first time it is triggered.
-
[data]
optionalArbitrary data that will be included as the second parameter when the listener is called.
-
[useCapture=false]
Boolean optionalFor events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
Returns:
Returns the anonymous function that was created and assigned as the listener. This is needed to remove the listener later using .removeEventListener.
removeAllEventListeners
-
[type]
Removes all listeners for the specified type, or all listeners of all types.
Example
// Remove all listeners
displayObject.removeAllEventListeners();
// 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
-
[useCapture]
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);
willTrigger
-
type
Indicates whether there is at least one listener for the specified event type on this object or any of its ancestors (parent, parent's parent, etc). A return value of true indicates that if a bubbling event of the specified type is dispatched from this object, it will trigger at least one listener.
This is similar to hasEventListener, but it searches the entire event flow for a listener, not just this object.
Parameters:
-
type
StringThe string type of the event.
Returns:
Returns true
if there is at least one listener for the specified event.