diff --git a/README.md b/README.md index eec0c28..be5e84a 100755 --- a/README.md +++ b/README.md @@ -2,13 +2,18 @@ _MicroEvent.js_ is a event emitter library which provides the [observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) to javascript objects. -It works on node.js and browser. It is a single .js file containing -a 20 lines class -(only 321-bytes after minification+gzip). +It works on node.js and browser. It is a single .js file containing with only a few lines. + +## Different from master branch +- obj.bind is now obj.on +- obj.unbind is now obj.off +- Added chainability `obj.on('a', fn).on('b', fn)` +- Added on all events function `on('*', function (eventName) { })` and `trigger('*')` +- Some code reformatted ## How to Use It -You need a single file [microevent.js](https://github.com/jeromeetienne/microevent.js/raw/master/microevent.js). +You need a single file [microevent.js](https://github.com/jeroenransijn/microevent.js/raw/master/microevent.js). Include it in a webpage via the usual script tag. ```html @@ -27,8 +32,8 @@ Now suppose you got a class `Foobar`, and you wish it to support the observer pa MicroEvent.mixin(Foobar) ``` -That's it. The repository contains an [example in browser](https://github.com/jeromeetienne/microevent.js/blob/master/examples/example.html) -and an [example in nodejs](https://github.com/jeromeetienne/microevent.js/blob/master/examples/example.js). +That's it. The repository contains an [example in browser](https://github.com/jeroenransijn/microevent.js/blob/master/examples/example.html) +and an [example in nodejs](https://github.com/jeroenransijn/microevent.js/blob/master/examples/example.js). Both use the same code in different contexts. Let me walk you thru it. ## Example @@ -75,7 +80,7 @@ notified date Tue, 22 Mar 2011 14:43:42 GMT ## Conclusion -MicroEvent.js is available on github here -under MIT license. +MicroEvent.js is available on github here +under MIT license. If you hit bugs, fill issues on github. Feel free to fork, modify and have fun with it :) diff --git a/microevent-debug.js b/microevent-debug.js index 2eb14f3..fb10f04 100755 --- a/microevent-debug.js +++ b/microevent-debug.js @@ -1,5 +1,6 @@ /** * MicroEvent.js debug + * -- Not updated yet * * # it is the same as MicroEvent.js but it adds checks to help you debug */ @@ -7,19 +8,19 @@ var MicroEvent = function(){} MicroEvent.prototype = { bind : function(event, fct){ - this._events = this._events || {}; + this._events = this._events || {}; this._events[event] = this._events[event] || []; this._events[event].push(fct); }, unbind : function(event, fct){ console.assert(typeof fct === 'function'); - this._events = this._events || {}; + this._events = this._events || {}; if( event in this._events === false ) return; console.assert(this._events[event].indexOf(fct) !== -1); this._events[event].splice(this._events[event].indexOf(fct), 1); }, trigger : function(event /* , args... */){ - this._events = this._events || {}; + this._events = this._events || {}; if( event in this._events === false ) return; for(var i = 0; i < this._events[event].length; i++){ this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1)) diff --git a/microevent.js b/microevent.js index 45ab5d9..61cbca2 100755 --- a/microevent.js +++ b/microevent.js @@ -1,32 +1,42 @@ /** * MicroEvent - to make any js object an event emitter (server or browser) - * + * ------------------------------------------------------ + * Edited by Jeroen Ransijn in august 2014 + * - obj.bind is now obj.on + * - obj.unbind is now obj.off + * - Added chainability `obj.on('a', fn).on('b', fn)` + * - Added on all events function `on('*', function (eventName) { })` and `trigger('*')` + * - Some code reformatted + * ------------------------------------------------------ * - pure javascript - server compatible, browser compatible * - dont rely on the browser doms * - super simple - you get it immediatly, no mistery, no magic involved - * - * - create a MicroEventDebug with goodies to debug - * - make it safer to use */ - -var MicroEvent = function(){}; -MicroEvent.prototype = { - bind : function(event, fct){ +var MicroEvent = function () {}; +MicroEvent.prototype = { + on: function (event, fct) { this._events = this._events || {}; - this._events[event] = this._events[event] || []; + this._events[event] = this._events[event] || []; this._events[event].push(fct); + return this; }, - unbind : function(event, fct){ + off: function (event, fct) { this._events = this._events || {}; - if( event in this._events === false ) return; + if ( ! (event in this._events) ) return this; this._events[event].splice(this._events[event].indexOf(fct), 1); + return this; }, - trigger : function(event /* , args... */){ + trigger: function (event) { this._events = this._events || {}; - if( event in this._events === false ) return; - for(var i = 0; i < this._events[event].length; i++){ - this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1)); + var argsAll = Array.prototype.slice.call(arguments, 0), args = argsAll.slice(1); + argsAll.unshift('*'); + if (event !== '*') this.trigger.apply(this, argsAll); + if ( ! (event in this._events) ) return this; + var eventsLength = this._events[event].length; + for (var i = 0; i < eventsLength; i++) { + this._events[event][i].apply(this, args); } + return this; } }; @@ -37,16 +47,16 @@ MicroEvent.prototype = { * * @param {Object} the object which will support MicroEvent */ -MicroEvent.mixin = function(destObject){ - var props = ['bind', 'unbind', 'trigger']; - for(var i = 0; i < props.length; i ++){ - if( typeof destObject === 'function' ){ +MicroEvent.mixin = function (destObject) { + var props = ['on', 'off', 'trigger']; + for (var i = 0; i < 3 /* props.length */; i++) { + if ( typeof destObject === 'function' ) { destObject.prototype[props[i]] = MicroEvent.prototype[props[i]]; - }else{ + } else { destObject[props[i]] = MicroEvent.prototype[props[i]]; } } -} +}; // export in common js if( typeof module !== "undefined" && ('exports' in module)){