Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overall update #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://github.com/jeromeetienne/microevent.js/blob/master/microevent.js#L12-31">20 lines class</a>
(only 321-bytes after minification+gzip).
It works on node.js and browser. <a href="https://github.com/jeroenransijn/microevent.js/blob/master/microevent.js">It is a single .js file containing with only a few lines.</a>

## 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
Expand All @@ -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
Expand Down Expand Up @@ -75,7 +80,7 @@ notified date Tue, 22 Mar 2011 14:43:42 GMT

## Conclusion

MicroEvent.js is available on github <a href='https://github.com/jeromeetienne/microevent.js'>here</a>
under <a href='https://github.com/jeromeetienne/microevent.js/blob/master/MIT-LICENSE.txt'>MIT license</a>.
MicroEvent.js is available on github <a href='https://github.com/jeroenransijn/microevent.js'>here</a>
under <a href='https://github.com/jeroenransijn/microevent.js/blob/master/MIT-LICENSE.txt'>MIT license</a>.
If you hit bugs, fill issues on github.
Feel free to fork, modify and have fun with it :)
7 changes: 4 additions & 3 deletions microevent-debug.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
/**
* MicroEvent.js debug
* -- Not updated yet
*
* # it is the same as MicroEvent.js but it adds checks to help you debug
*/

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))
Expand Down
52 changes: 31 additions & 21 deletions microevent.js
Original file line number Diff line number Diff line change
@@ -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;
}
};

Expand All @@ -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)){
Expand Down