Giter VIP home page Giter VIP logo

eventdispatcher.js's Introduction

eventDispatcher

JavaScript events for custom objects

Build Status npm version

Install

npm

$ npm install gr-event-dispatcher

API

eventDispatcher~addEventListener(type, listener) ⇒ Object

Registers the specified listener on the event target it's called on.

Kind: inner method of eventDispatcher
Returns: Object - The eventDispatcher object.
Throws:

  • TypeError If the object that eventDispatcher is applied to is not extensible.
Param Type Description
type String A string representing the event type to listen for.
listener function A function to be executed when an event of the specified type occurs.

eventDispatcher~hasEventListener(type, listener) ⇒ Boolean

Checks if the target object has a listener registered on for specific event type.

Kind: inner method of eventDispatcher
Returns: Boolean - True if target object has listener registered for specific event type; otherwise false.

Param Type Description
type String A string representing the event type.
listener function The event listener to check if registered for the specified event type.

eventDispatcher~removeEventListener(type, listener) ⇒ Object

Removes the previously registered event listener from the event target.

Kind: inner method of eventDispatcher
Returns: Object - The eventDispatcher object.

Param Type Description
type String A string representing the event type to remove.
listener function The event listener function to remove from the event target.

eventDispatcher~dispatchEvent(event) ⇒ Object

Dispatches an event at the specified event target.

Kind: inner method of eventDispatcher
Returns: Object - The eventDispatcher object.

Param Type Description
event Object The event object to be dispatched.

eventDispatcher~apply(object) ⇒ Object

Applies the eventDispatcher prototype methods to the event target.

Kind: inner method of eventDispatcher
Returns: Object - The eventDispatcher object.
Throws:

  • TypeError If the object that eventDispatcher is applied to is not extensible.
Param Type Description
object Object The event target object.

Example

var car = {
  start: function (afterStartCallback) {
    // Dispatch `start` event.
    this.dispatchEvent({type: 'start', message: 'Start engines'});
    afterStartCallback && afterStartCallback(this);
  },
  stop: function () {
    // Dispatch `stop` event.
    this.dispatchEvent({type: 'stop', message: 'Stop engines'});
  }
};

function startEngines(event) {
  console.log(event.message);
}

function stopEngines(event) {
  console.log(event.message);
}

// Apply `eventDispatcher` methods to the prototype of the event target.
eventDispatcher.apply(Object.getPrototypeOf(car));

// Register event listeners to event target.
car
  .addEventListener('start', startEngines)
  .addEventListener('afterStart', startEngines)
  .addEventListener('stop', stopEngines);

// `start` event fires first, then `afterStart` and finally
// after two seconds `stop` events fires.

car.start(function (self) {
  // Dispatch `afterStart` event.
  self.dispatchEvent({type: 'afterStart', message: 'Car has started'});
});

console.log(car.hasEventListener('start', startEngines));      // -> true
console.log(car.hasEventListener('afterStart', startEngines)); // -> true
console.log(car.hasEventListener('stop', startEngines));       // -> false
console.log(car.hasEventListener('stop', stopEngines));        // -> true

setTimeout(function () {
  car.stop();

  // Remove all event listeners from event target.
  car
    .removeEventListener('start', startEngines)
    .removeEventListener('afterStart', startEngines)
    .removeEventListener('stop', stopEngines);
}, 2000);

License

The MIT License (MIT)

eventdispatcher.js's People

Contributors

georapbox avatar

Stargazers

 avatar

Watchers

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