Skip to content

jimgswang/EventEmitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventEmitter Build Status

Platform agnostic Node style EventEmitters. Bringing events to the browser without any other dependencies! Supports AMD, CommonJS and global export

var emitter = new EventEmitter();
emitter.on('evt', function(data) {
    console.log(data);
});

emitter.emit('evt', {foo: 'bar' });

var fn = function();
emitter.on('evt', fn);

emitter.removeListener('evt', fn);
emitter.removeAllListeners();

Inherit

function MyClass() {
    EventEmitter.call(this);
}

MyClass.prototype = Object.create(EventEmitter.prototype);
MyClass.prototype.constructor = MyClass;

var instance = new MyClass();
instance.once('evt', function(data) {
    console.log(data);
});

instance.emit('evt', 'this will be logged');
instance.emit('evt', 'this will not');

API

EventEmitter supports the full API specified at http://nodejs.org/api/events.html with two exceptions: There is no concept of Node's domains, and no .setMaxListeners - add as many as you want (be responsible).

Legacy browsers

This library uses two ECMAScript5 functions. If you want to use it in older environments, then polyfill Function.prototype.bind and Array.prototype.forEach available at MDN here and here

About

Platform agnostic Node style EventEmitters

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published