API Documentation for: 0.4.1
Show:

EventDispatcher Class

Defined in: EventDispatcher:34
Module: TweenJS

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

()

Defined in EventDispatcher:34

Methods

addEventListener

(
  • type
  • listener
)
Function | Object

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 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.

Returns:

Function | Object: Returns the listener for chaining or assignment.

dispatchEvent

(
  • eventObj
  • [target]
)
Boolean

Defined in dispatchEvent:181

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 | String

    An 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 optional

    The object to use as the target property of the event object. This will default to the dispatching object.

Returns:

Boolean: Returns true if any listener returned true.

hasEventListener

(
  • type
)
Boolean

Indicates whether there is at least one listener for the specified event type.

Parameters:

  • type String

    The string type of the event.

Returns:

Boolean: Returns true if there is at least one listener for the specified event.

initialize

(
  • target
)
static

Defined in initialize:74

Static initializer to mix in EventDispatcher methods.

Parameters:

  • target Object

    The target object to inject EventDispatcher methods into. This can be an instance or a prototype.

initialize

() protected

Defined in initialize:98

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 optional

    The 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);

Parameters:

  • type String

    The string type of the event.

  • listener Function | Object

    The listener function or object.

toString

() String

Defined in toString:231

Returns:

String: a string representation of the instance.

Properties

_listeners

Object protected

Defined in _listeners:90