An Event Emitter forked from Backbone.js
My framework of choice is backbone.js and it uses Backbone.Events to implement Event Emitters.
var mySimpleObject =name: "John"surname: "Doe"gender: "Male";// use underscore to extend mySimpleObject with Backbone.Events_extendmySimpleObject BackboneEvents;mySimpleObjecton'customEvent'// do something cool;mySimpleObjecttrigger'customEvent';
In the above example we simply extend a normal object with Backbone.Events to turn it into an Event Emitter.
Node.js also has a powerful EventEmitter object that can be used to add some awesomeness to objects.
var util = require'util';var EventEmitter = require'events'EventEmitter;// here is an ordinary object representing my Dogvar myDog =name: 'Spotty'likes: 'bones' 'biskuits'ate:thisatefood;// Add some magic to my ordinary objectutilinheritsmyDog EventEmitter;// Now my object can listen to and emit eventsmyDogon'hungry'myDogfeed'bone';myDogemit'hungry';
Using Event Emitters has many advantages; you can emit events when an object's state changes, objects can eavesdrop on other objects, reduces callback nesting, e.t.c.
An Event Emitter would have the same API as Object in ES5.
// an emptyvar emitter1 = EventEmittercreatenull;var mySimpleObject =name: "John"surname: "Doe"gender: "Male";var emitter2 = EventEmittercreatemySimpleObject;
Since native Event Emitters are just but a wishlist for now, I have forked Backbone Events and created a small library called Emitter. You can drop it into your project to start messing with Event Emitters.
Emitter uses the same API as Backbone.Events. The only different is that I have renamed the
trigger method to
emit. Below is an example of how to use Emitter.
"use strict";// in Node require the modulevar Emitter = require'emitter';// In the browser include the script in your html// <script src="dest/emitter.min.js"></script>var Dog = Emitterextendthisemit'buck';;var Person = Emitterextendthisemit'run';thisrunning = true;;var intruder = ;var myPet = ;myPeton'buck'console.log'My dog is bucking';;intruderon'run'console.log'The intruder is rinning away';;intruderlistenTomyPet 'buck' intruderrun;myPetbuck;
npm install emiter
npm run-script example1npm run-script example2
Copyright (c) 2013 Qawelesizwe Mlilo firstname.lastname@example.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.