API Documentation for: 0.4.0
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.

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:154

Dispatches the specified event.

Parameters:

  • eventObj Object | String

    An object with a "type" property, or a string type. If a string is used, dispatchEvent will contstruct a generic event object with "type" and "params" properties.

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

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.

Parameters:

  • type String

    The string type of the event.

  • listener Function | Object

    The listener function or object.

toString

() String

Defined in toString:191

Returns:

String: a string representation of the instance.

Properties

_listeners

Object protected

Defined in _listeners:90