Emitter

Moduleejs
Definition class Emitter
InheritanceEmitter inherit Object
StabilityPrototype.
Example
events.on(event, function (event, ...args) {
    //  Do something
}).fire("topic", 1, 2, 3)
@stability prototype

The emitter class provides a publish/subscribe model of communication via events.

It supports the registration of observers who want to subscribe to events of interest.


Properties

(No own properties defined)


Emitter Class Methods

(No own class methods defined)

QualifiersMethod

Emitter Instance Methods

QualifiersMethod
Emitter()
 Construct a new event Emitter object.
clearObservers(name: Object = null): Void
 Clear observers for a given event name.
fire(name: String, args: Array): Void
 Emit an event to the registered observers.
getObservers(name: String): Array
 Return the observers for this emitter.
fireThis(name: String, thisObj: Object, args: Array): Void
 Emit an event to the registered observers using an explict value for the "this" object.
hasObservers(): Boolean
 Determine if the emitter has any observers.
off(name: Object, callback: Function): Void
 Turn off and remove a registered observer.
on(name: Object, callback: Function): Emitter
 Add an observer for a set of named event.

Method Detail

Emitter()
Description
Construct a new event Emitter object.

clearObservers(name: Object = null): Void
Description
Clear observers for a given event name.
Parameters
name: Object Event name to clear. The name can be a string or an array of event strings. If null, observers for all event names are cleared. [default: null]

fire(name: String, args: Array): Void
Description
Emit an event to the registered observers.
Parameters
name: String Event name to fire to the observers.
args: Array Args to pass to the observer callback.

getObservers(name: String): Array
Description
Return the observers for this emitter.
Parameters
name: String Event name to fire to the observers.
Returns
An array of observer endpoints. These are cloned and not the actual observer objects. 8.

fireThis(name: String, thisObj: Object, args: Array): Void
Description
Emit an event to the registered observers using an explict value for the "this" object.
Parameters
name: String Event name to fire to the observers.
thisObj: Object Object to use for "this" when running the callback. This overrides any bound values for "this".
args: Array Args to pass to the observer callback.

hasObservers(): Boolean
Description
Determine if the emitter has any observers.
Returns
True if there are currently registered observers.

off(name: Object, callback: Function): Void
Description
Turn off and remove a registered observer.
Parameters
name: Object Event name used when the observer was added.
callback: Function Callback function used when the observer was added. If null is supplied, all callbacks will be removed.

on(name: Object, callback: Function): Emitter
Description
Add an observer for a set of named event. The callback will be invoked when the requested event is fired by calling Emitter.fire. When the callback runs, it will be invoked with the value of "this" relevant to the context of the callback. If the callback is a class method, the value of "this" will be the object instance. Global functions will have "this" set to the global object. Use Function.bind to override the bound "this" value.
Parameters
name: Object Event name to observe. The observer will receive events of this event class or any of its subclasses. The name can be a string or an array of event strings.
callback: Function Function to call when the event is received.