Giter Site home page Giter Site logo

emitjs's Introduction

Overview

emitJS is a combination of a pub/sub system and an event emitter. Taking some of the best parts of js-signals and the simplicity of EventEmitter and combining them so either (or both) pattern implementation can be used.

Simple Emitter Example

  // Create an emitter.
  var emitter = new emit.EventEmitter();
  
  // Add a listener to the emitter.
  emitter.add('userCreated', function(){console.log('user has been created');});
  
  // Add another listener to the emitter.
  emitter.add('userCreated', function(){alert('user has been created');});
  
  // Emit the event
  emitter.emit('userCreated');
  // => Alert shown displaying 'user has been created';
  // => console message logged 'user has been created';

Simple Binding Example

  // Create the event bindings.
  var my = {
    events: {
      userCreated: new emit.Binding(),
      userRemoved: new emit.Binding()
    }
  };
  
  // Add a listener to the userCreated binding.
  my.events.userCreated.add(function(){console.log('user has been created');});
  
  // Emit the userCreated event.
  my.events.userCreated.emit();
  // => console message logged 'user has been created'

More Examples

To see more examples please see the Examples Page in the wiki.

Should I use Emitter or Binding?

Short answer is whichever you want. Both have their pros and cons and Miller Medeiros has a great write up here comparison between different observer pattern implmentations.

I tend to prefer the binding implementation just because it works well with auto complete and my tests will complain when I try and emit or subscribe to the wrong event.

emitjs's People

Stargazers

 avatar Cave HEX avatar

emitjs's Issues

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.