GitXplorerGitXplorer
n

emitter.js

public
85 stars
7 forks
2 issues

Commits

List of commits on branch master.
Unverified
643192118c55dbcf97ddeeeb88467fa57cc31728

README: unmaintained

nnecolas committed 9 years ago
Unverified
a9f41d0123a95cced85d8d1a981765272ede7bc1

Fix package.json

nnecolas committed 11 years ago
Unverified
e2fa9a7d9ae0dd4407c5283cc1cf3e1552abcf75

Add 'repository' field to package.json

nnecolas committed 11 years ago
Unverified
ba3e9e6dd11bad99e6641ee7678f82ac95dc0964

4 spaces -> 2 spaces

nnecolas committed 11 years ago
Unverified
b2124f624dd59624c4ac4194dbfec0a4220a2ce1

Replace UMD module format with CommonJS

nnecolas committed 11 years ago
Unverified
c2e4c9310a77e58a86837f01bc1c9c98760791b0

v1.1.0

nnecolas committed 11 years ago

README

The README file for this repository.

emitter.js

unmaintained

Build Status

JavaScript event-emitter.

Installation

npm: npm install emitter.js Component(1): component install necolas/emitter.js Bower: bower install emitter.js

API

Emitter(obj)

The Emitter function can be used as a Constructor or as a mixin.

As an Emitter instance:

var Emitter = require('emitter');
var foo = new Emitter;
foo.trigger('foo');

As a mixin:

var Emitter = require('emitter');
var foo = {};
Emitter(foo);

foo.trigger('bar');

As a prototype mixin:

var Emitter = require('emitter');
var Foo = function () {};
Emitter(Foo.prototype);

Emitter#on(event, callback)

Register an event handler callback. Protects against duplicate handlers being registered.

var handler = function () {
  console.log('The function `handler` has been registered for the event `foo`.');
};

foo.on('foo', handler);

Emitter#once(event, callback)

Register a one-off event handler callback, removed immediately after it is invoked the first time.

var handler = function () {
  console.log('The function `handler` has been registered as a one-off callback for the event `foo`.');
};

foo.once('foo', handler);

Emitter#off([event], [callback])

Remove the event handler callback. If no callback is specified, it will remove all callbacks for the event. If no event is specified, the entire event registry will be deleted.

var handler = function () {
  console.log('Registered for the event `foo`.');
};

foo.off('foo', handler);
foo.off('foo');
foo.off();

Emitter#trigger(event, [...])

Trigger an event with optional arguments. Alias: emit.

var data = { name: 'nicolas' };
foo.trigger('foo', data);

Emitter#getListeners(event)

Return an array of callbacks registered for the event, or an empty array. Initializes the registry if necessary.

foo.getListeners('foo');

Emitter#hasListeners(event)

Check if any callbacks are registered for the event.

foo.hasListeners('foo');

Testing

Install and run the test suite:

make

Re-run the test suite:

make test

Browser support

  • Google Chrome (latest)
  • Opera (latest)
  • Firefox 4+
  • Safari 5+
  • Internet Explorer 8+