EventEmitter Class
Represents an object that can bind event handlers to arbitrary event names and fire events.
Inheriting this class gives a descendant the ability to register and fire arbitrary events. You can inherit this class by using the Function.call method of the EventEmitter constructor in your class's constructor (see the example below).
Constructor
EventEmitter
()
Example:
function Descendant() {
EventEmitter.call(this);
this.otherMethod = function () {
// ...
};
// other methods and properties
}
var object = new Descendant();
object.on('salute', function () {
alert('Hello, world!');
});
object.emit('salute');
Methods
bind
-
name
-
handler
-
[scope]
Binds the specified event handler to the specified event name.
The event name can contain any characters.
emit
-
name
-
[arguments]
An alias for trigger.
Parameters:
-
name
StringThe name of the event to fire.
-
[arguments]
Any optional multipleZero or more arguments to forward to the handlers.
once
-
name
-
handler
-
[scope]
Binds the specified event handler to the specified event name.
The event name can contain any characters.
The difference with on is that handlers registered with this method are executed at most once and then automatically unbound.
register
-
name
Adds a name
method to this object. The new method can bind or trigger
the name
event.
The new method is chainable and has two signatures, corresponding to the bind/on and trigger/emit methods.
Parameters:
-
name
ObjectString The name of the new method and event to handle.
Example:
emitter.register('salute');
// binds a handler to the 'salute' event
emitter.salute(function () {
alert('Hello, world!');
});
emitter.salute(); // triggers the 'salute' event
trigger
-
name
-
[arguments]
Fires the specified event.
You can optionally pass any number of arguments that will be forwarded to the handlers.
Parameters:
-
name
StringThe name of the event to fire.
-
[arguments]
Any optional multipleZero or more arguments to forward to the handlers.