eventful
Event-based javascript.
My personal flavor of a javascript event emitter. It can be used to listen to and emit events for event-based behavior in the browser or in node.js. This readme contains installation instructions and an overview of features.
// initialize an event emittervar ee = ; // create a callback functionvar { console;}; // register the callbackee; // emit the eventee; // #=> "The foo event was emitted."
Detailed API documentation will be coming soon.
Installation
Downloads:
How to use in a browser:
<!-- direct link -->
<script type='text/javascript' src='https://raw.github.com/AlphaHydrae/eventful/master/src/eventful.min.js'></script>
<!-- with downloaded file -->
<script type='text/javascript' src='/assets/js/eventful.min.js'></script>
How to install with npm:
npm install eventful-ah
How to use in node:
// if installed with npm
var EventEmitter = require('eventful-ah');
// if downloaded
var EventEmitter = require('./lib/eventful.js');
Features
Jump to:
ee;
You can remove callbacks with the off
method.
var { console;}; var { console;}; // register both callbacksee; // you can remove a specific callbackee; // or you can remove all callbacks for the foo eventee; // nothing happens any more with the foo eventee; // the bar callback is still registeredee; // #=> "The bar event was emitted." // finally, you can also remove all callbacks like thisee; // nothing happens with any eventee;
When emitting events, any additional arguments after the event name are passed to registered callbacks.
ee; ee; // #=> "I was called with bar."
The on
method can be used to retrieve the list of callbacks registered for a given event.
// register your callbacksee; // get the list of registered callbacksvar callbacks = ee; // check its contentsconsole; // #=> 2
You can register callback functions in a namespace, so that only events emitted with the same namespace will call them.
// create an event emittervar nee = ; // register a callback in a namespacenee; // the callback is not called if you emit bar in another namespace or with no namespacenee;nee; // it is only called if you emit bar within the same namespacenee; // #=> "The bar event was emitted in the foo namespace."
Note that callbacks registered with no namespace will be called when emitting an event in any namespace.
// register a callback on kung in no namespacenee; // the callback with be called by emitting kung in any namespacenee; // #=> "The kung event was emitted."nee; // #=> "The kung event was emitted."
License (MIT)
Copyright (c) 2011 Alpha Hydrae
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.