Class: Emitter

.base. Emitter

A class representing an Event Emitter

new Emitter()

Create a new Event Emitter
Examples
// The Model class will inherit from Emitter
class Model extends Emitter {
  ...
}
// The Collection class will have both Array and Emitter methods, and can be subclassed further
class Collection = Emitter.extend(Array) {
  ...
}

Methods


<static> extend( [Superclass])

Mixin the Event Emitter into another class. This method takes the provided class, adds the Emitter methods to it, and returns a new class that you can then subclass further.
Parameters:
Name Type Argument Description
Superclass function <optional>
A class constructor to mixin the Emitter with
Returns:
Returns the new class with the Emitter methods mixed in
Type
function
Example
const Collection = Emitter.extend(Array);

emit(event [, args])

Triggers the callbacks for a given event. Subsequent arguments will be passed to the event callbacks.
Parameters:
Name Type Argument Description
event String The event name to trigger
args Any <optional>
<repeatable>
Additional arguments to pass to the event callback
Returns:
Returns a Promise that resolves when all callbacks are complete
Type
Object

off( [event] [, callback])

Unsubscribe from one or more events on this object
Parameters:
Name Type Argument Description
event String <optional>
The event to remove. If none is specified, all events will be removed.
callback function <optional>
The callback to remove. If none is specified, all callbacks for the specified event will be removed.
Returns:
Returns the Emitter object
Type
Object

on(event, callback)

Subscribe to an event on this object, by binding a callback function to the name of the event. Callbacks may be Promises or regular functions.
Parameters:
Name Type Description
event String | Object The name of the event to subscribe to. May also be an object mapping event names to callback functions
callback function The callback function to invoke whenever the specified event is fired
Returns:
Returns the emitter object
Type
Object

once(event, callback)

Subscribe to an event on this object for one time only. After the event is triggered, the callback will be removed. Callbacks may be Promises or regular functions.
Parameters:
Name Type Description
event String The name of the event to subscribe to
callback function The callback function to invoke when the event is fired
Returns:
Returns the emitter object
Type
Object