Skip to content

quarterpast/Eventide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.