JavaScript event-emitter.
npm: npm install emitter.js
Component(1): component install necolas/emitter.js
Bower: bower install emitter.js
The Emitter
function can be used as a Constructor or as a mixin.
As an Emitter instance:
var Emitter = require('emitter');
var foo = new Emitter;
foo.trigger('foo');
As a mixin:
var Emitter = require('emitter');
var foo = {};
Emitter(foo);
foo.trigger('bar');
As a prototype mixin:
var Emitter = require('emitter');
var Foo = function () {};
Emitter(Foo.prototype);
Register an event
handler callback
. Protects against duplicate handlers
being registered.
var handler = function () {
console.log('The function `handler` has been registered for the event `foo`.');
};
foo.on('foo', handler);
Register a one-off event
handler callback
, removed immediately after it is
invoked the first time.
var handler = function () {
console.log('The function `handler` has been registered as a one-off callback for the event `foo`.');
};
foo.once('foo', handler);
Remove the event handler callback
. If no callback
is specified, it will
remove all callbacks for the event
. If no event
is specified, the entire
event registry will be deleted.
var handler = function () {
console.log('Registered for the event `foo`.');
};
foo.off('foo', handler);
foo.off('foo');
foo.off();
Trigger an event
with optional arguments. Alias: emit
.
var data = { name: 'nicolas' };
foo.trigger('foo', data);
Return an array of callbacks registered for the event
, or an empty array.
Initializes the registry if necessary.
foo.getListeners('foo');
Check if any callbacks are registered for the event
.
foo.hasListeners('foo');
Install and run the test suite:
make
Re-run the test suite:
make test
- Google Chrome (latest)
- Opera (latest)
- Firefox 4+
- Safari 5+
- Internet Explorer 8+