EventEmitter

EventEmitter

This module contains mixin methods for handling events.

Example

// assign `EventEmitter` methods to `MyClass.prototype`
tools.mixin(MyClass.prototype, EventEmitter);

Methods

<static> addListener(type, context, listener) → {Object}

Registers a single listener for the specified event type(s). Listeners are executed with the `this` binding of the host object.
Parameters:
Name Type Argument Default Description
type String The event type.
context <optional>
null The context to call the listener in. Defaults to the event emitter itself.
listener Function The function called when the event occurs.
Returns:
The host object.
Type
Object
Example
// register a listener for an event type
object.addListener('blur', listener);
// register a listener for multiple event types
object.addListener('blur focus', listener);

<static> emit(type) → {Object}

Executes all registered listeners of the specified event type.
Parameters:
Name Type Argument Description
type String The event type.
args1, ... Mixed <optional>
Any number of arguments to pass to the listener.
Returns:
The host object.
Type
Object

<static> emitAsync(type) → {Object}

Executes all registered listeners of the specified event type after the current execution block completes (i.e. asynchronously)
Parameters:
Name Type Argument Description
type String The event type.
args1, ... Mixed <optional>
Any number of arguments to pass to the listener.
Returns:
The host object.
Type
Object

<static> listeners(type) → {Array}

Returns an array of event listeners for a given type that can be manipulated to add or remove listeners.
Parameters:
Name Type Description
type String The event type.
Returns:
The listeners array.
Type
Array

<static> on(type, listener) → {Object}

An alias for `addListener`.
Parameters:
Name Type Description
type String The event type.
listener Function The function called when the event occurs.
Returns:
The host object.
Type
Object

<static> once(type, context, listener) → {Object}

Registers a listener that will only be executed one time.
Parameters:
Name Type Argument Default Description
type String The event type.
context <optional>
null The context to call the listener in. Defaults to the event emitter itself.
listener Function The function called when the event occurs.
Returns:
The host object.
Type
Object

<static> removeAllListeners(type) → {Object}

Unregisters all listeners or those for the specified event type(s).
Parameters:
Name Type Argument Description
type String <optional>
The event type. Unregisters all listeners if no event type is given.
Returns:
The host object.
Type
Object
Example
// unregister all listeners
object.removeAllListeners();
// unregister all listeners for an event type
object.removeAllListeners('click');
// unregister all listeners for multiple event types
object.removeAllListeners('click mousedown touch');

<static> removeListener(type, context, listener) → {Object}

Unregisters a single listener for the specified event type(s).
Parameters:
Name Type Argument Default Description
type String The event type.
context <optional>
null The context the listener was registered with.
listener Function The function to unregister.
Returns:
The host object.
Type
Object
Example
// unregister a listener for an event type
object.removeListener('blur', listener);
// unregister a listener for multiple event types
object.removeListener('blur focus', listener);