Code Monkey home page Code Monkey logo

eventemitter's Introduction

Build Status

EventEmitter

Simple Interface for Publishing JavaScript Events.

Features

  • Operates as a standalone object.
  • Easy to make existing classes EventEmitters.
  • Available in all JavaScript runtime environments.
  • AMD/CommonJS compatible.
  • Small set of memorable methods: on, off and emit. That's it.
  • Standardized signature for event listeners: function(sender, args).
  • Event namespacing.
  • Chainable API for easy and elegant use.

Usage

Standalone Usage:

var emitter = new EventEmitter();

Add Listeners:

var listener = function() {};
emitter.on('event', listener);                 // Add listener for event.
emitter.on('event', listener, { once: true }); // Add one-time listener.
emitter.on('event.namespace', listener);       // Add listener for namespaced event.

Remove Listeners:

emitter.off();                // Remove all listeners for all events.
emitter.off('foo');           // Remove all listeners for event 'foo', including child namespaces.
emitter.off('foo', listener); // Remove a specific listener for event 'foo'.
emitter.off('foo.namespace'); // Remove all listeners for namespaced event.
emitter.off('.namespace');    // Remove all event listeners with 'namespace'.

Emit Events:

var args = { prop: 'value' };
emitter.emit('event');                        // Emit event with no args.
emitter.emit('event', args);                  // Emit event with args.
emitter.emit('event', args, { async: true }); // Emit event asynchronously.

Chain Everything:

emitter
  .on('foo', function() { console.log('foo'); })
  .emit('foo') // -> 'foo'
  .off('foo')
  .emit('foo'); // ->

Extend Classes With EventEmitter:

// Add EventEmitter behaviors to your classes:
function Thing() {}
EventEmitter.extend(Thing.prototype);

// Now 'Thing' acts as an EventEmitter:
Thing.prototype.setName = function(name) {
  this.name = name;
  this.emit('named', { name: name });
};

var thing = new Thing();
thing.on('named', function(sender, args) {
  console.log('The thing is called: ' + args.name);
});
thing.setName('banana');
// -> 'The thing is called: banana'

eventemitter's People

Contributors

mccalltd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.