Skip to content

Latest commit

 

History

History
61 lines (45 loc) · 1.61 KB

readme.md

File metadata and controls

61 lines (45 loc) · 1.61 KB

Eventide Build Status

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.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
	...

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.