Emitter
JavaScript events are very powerful, they give you the power to capture real world events in real time. For example, when a user moves their mouse in the browser or when they click.
Using JavaScript events to interact with DOM objects defined Web 2.0 and is still widely used (thanks to jQuery and similar libraries) but it has its limitations, the events are predefined and tied to the browser BOM and DOM.
A more powerful implementation of JavaScript Events is in Event Emitters. Most of the popular JavaScript libraries/frameworks have their own implementation of Event Emitters.
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_; mySimpleObject; mySimpleObject;
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 = ;var EventEmitter = EventEmitter; // here is an ordinary object representing my Dogvar myDog = name: 'Spotty' likes: 'bones' 'biskuits' ate: { this } ; // Add some magic to my ordinary objectutil; // Now my object can listen to and emit eventsmyDog; myDog;
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.
The more I use event emitters the more I feel that they should become part of a future version of JavaScript
My proposed API
An Event Emitter would have the same API as Object in ES5.
// an emptyvar emitter1 = EventEmitter; var mySimpleObject = name: "John" surname: "Doe" gender: "Male"; var emitter2 = EventEmitter;
Introducing Emitter.
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 = ; // In the browser include the script in your html // <script src="dest/emitter.min.js"></script> var Dog = Emitter; var Person = Emitter; var intruder = ;var myPet = ; myPet; intruder; intruder; myPet;
Installation
npm install emiter
Examples
npm run-script example1
npm run-script example2
Testing
npm test
(MIT License)
Copyright (c) 2013 Qawelesizwe Mlilo qawemlilo@gmail.com
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.