Namespaced events as a mixin
npm install eventide
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.on('eaten:banana', function() {
console.log('yuck');
});
matt.eat('peanut butter'); // => "mmm, peanut butter"
matt.eat('banana'); // => "yuck"
Or with Livescript's implements
:
class Person implements eventide
...
Registers handler
to handle event
.
Removes event handlers. If event
is given, removes all handlers for event
. If both event
and handler
are given, removes that particular event handler.
Like on
, but removes the handler when the event has fired.
Triggers when any event is emitted. The first argument is the event name.
Remove a particular handler for onAny
, or all handlers.
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.
MIT.