Skip to content

Commit

Permalink
Event emitters in the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
boolpath committed Mar 22, 2014
0 parents commit f2b651b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions browser-events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src='browser-events.js'></script>
25 changes: 25 additions & 0 deletions browser-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var BrowserEmitter = function () {
var emitter = {
events: []
};

emitter.on = (function (event, callback) {
if (!(this.events[event] instanceof Array)) {
this.events[event] = [];
}
return this.events[event].push(callback) - 1;
}).bind(emitter);

emitter.emit = (function (event) {
var callbacks = this.events[event];
if (callbacks) {
callbacks.forEach(function (callback) {
if (callback instanceof Function) {
callback();
}
});
}
}).bind(emitter);

return emitter;
}
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "browser-events",
"description": "Event emitters in the browser",
"homepage": "https://github.com/jorgezaccaro/browser-events",
"author": {
"name": "Jorge Zaccaro",
"email": "jorgezaccaro@gmail.com",
"url": "https://github.com/jorgezaccaro"
},
"version": "0.0.1",
"license": "MIT",
"depedencies": {},
"keywords": ["browser", "events", "emitters"],
"main": "browser-events.js"
}

0 comments on commit f2b651b

Please sign in to comment.