Constructor
new EventDispatcher()
- Source:
Example
// add EventDispatcher capabilities to the "MyClass" class.
EventDispatcher.initialize(MyClass.prototype);
// Add an event.
instance.addEventListener("eventName", event => console.log(event.target + " was clicked."));
// scope ("this") can be be a challenge with events.
// using the core.EventDispatcher#on method to subscribe to events simplifies this.
instance.addEventListener("click", event => console.log(instance === this)); // false, scope is ambiguous.
instance.on("click", event => console.log(instance === this)); // true, `on` uses dispatcher scope by default.
Methods
(static) initialize(target)
- Source:
Static initializer to mix EventDispatcher methods into a target object or prototype.
Example
EventDispatcher.initialize(MyClass.prototype); // add to the prototype of the class
EventDispatcher.initialize(myInstance); // add to a specific instance
Parameters:
Name | Type | Description |
---|---|---|
target |
Object | The target object to inject EventDispatcher methods into. |
addEventListener(type, listener, useCaptureopt) → {function|Object}
- Source:
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", event => console.log('clicked', event));
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
string | The string type of the event. |
||
listener |
function | Object | An object with a handleEvent method, or a function that will be called when the event is dispatched. |
||
useCapture |
boolean |
<optional> |
false
|
For events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase. |
Returns:
Returns the listener for chaining or assignment.
- Type
- function | Object
dispatchEvent(eventObj, bubblesopt, cancelableopt) → {boolean}
- Source:
Dispatches the specified event to all listeners.
Example
// use a string event
this.dispatchEvent("complete")
// use an Event instance
const event = new createjs.Event("progress");
this.dispatchEvent(event);
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
eventObj |
Object | Event | string | An 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 |
<optional> |
false
|
Specifies the |
cancelable |
boolean |
<optional> |
false
|
Specifies the |
Returns:
Returns false if preventDefault()
was called on a cancelable event, true otherwise.
- Type
- boolean
hasEventListener(type) → {boolean}
- Source:
Indicates whether there is at least one listener for the specified event type.
Parameters:
Name | Type | Description |
---|---|---|
type |
string | The string type of the event. |
Returns:
Returns true if there is at least one listener for the specified event.
- Type
- boolean
off(type, listener, useCaptureopt)
- Source:
A shortcut to the removeEventListener method, with the same parameters and return value. This is a companion to the
on
method.
To remove a listener added with on
, you must pass in the returned wrapper function as the listener. See
core.EventDispatcher#on for an example.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
string | The string type of the event. |
||
listener |
function | Object | The listener function or object. |
||
useCapture |
boolean |
<optional> |
false
|
For events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase. |
on(type, listener, scopeopt, onceopt, dataopt, useCaptureopt) → {function}
- Source:
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
).
To remove a listener added with on
, you must pass in the returned wrapper function as the listener, or use
core.Event#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
const 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:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
string | The string type of the event. |
||
listener |
function | Object | An object with a handleEvent method, or a function that will be called when the event is dispatched. |
||
scope |
Object |
<optional> |
null
|
The 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 |
boolean |
<optional> |
false
|
If true, the listener will remove itself after the first time it is triggered. |
data |
* |
<optional> |
{}
|
Arbitrary data that will be included as the second parameter when the listener is called. |
useCapture |
boolean |
<optional> |
false
|
For 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.
- Type
- function
removeAllEventListeners(typeopt)
- Source:
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:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
string |
<optional> |
null
|
The string type of the event. If omitted, all listeners for all types will be removed. |
removeEventListener(type, listener, useCaptureopt)
- Source:
Removes the specified event listener.
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);
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
type |
string | The string type of the event. |
||
listener |
function | Object | The listener function or object. |
||
useCapture |
boolean |
<optional> |
false
|
For events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase. |
toString() → {String}
- Source:
Returns:
a string representation of the instance.
- Type
- String
willTrigger(type) → {boolean}
- Source:
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 core.EventDispatcher#hasEventListener, but it searches the entire event flow for a listener, not just this object.
Parameters:
Name | Type | Description |
---|---|---|
type |
string | The string type of the event. |
Returns:
Returns true
if there is at least one listener for the specified event.
- Type
- boolean