eventide
v0.2.0
Published
events mixin
Downloads
3
Readme
Eventide
Namespaced events as a mixin
Installation
npm install eventide
Usage
Using your favourite way of mixing in objects, add eventide
to an object. For example, using _.extend
:
function Person() {}
_.extend(Person.prototype, eventide, {
eat: function(food) {
this.emit('eaten:' + food);
}
});
var matt = new Person;
matt.on('eaten', function(food) {
console.log('mmm, ' + food);
});
matt.eat('peanut butter'); // => "mmm, peanut butter"
Or with Livescript's implements
:
class Person implements eventide
...
API
.on(event, handler)
Registers handler
to handle event
.
.off([event, [handler]])
Removes event handlers. If event
is given, removes all handlers for event
. If both event
and handler
are given, removes that particular event handler.
.once(event, handler)
Like on
, but removes the handler when the event has fired.
.onAny(handler)
Triggers when any event is emitted. The first argument is the event name.
.offAny([handler])
Remove a particular handler for onAny
, or all handlers.
What's wrong with EventEmitter (2)?
Node's built-in EventEmitter and EventEmitter2 are great and all, but both require subclassing to use with your own objects. Eventide is a plain object, and its functions perform their own setup. Just mix in to whatever.
Licence
MIT. © 2014 Matt Brennan.