Created
October 31, 2010 17:44
-
-
Save omarkj/656894 to your computer and use it in GitHub Desktop.
Simple EventBus in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var EventBus = function() { | |
var events = []; | |
var p_on = function(eventName, callback) { | |
if (!events.some(function(e) { | |
return e.name == eventName; })) { | |
var new_event = { | |
name: eventName, | |
cb: callback | |
}; | |
events.push(new_event); | |
return [eventName, callback]; | |
} | |
}; | |
var p_emit = function(eventName, args) { | |
var match = events.filter(function(e) { | |
return e.name == eventName; | |
}); | |
match.forEach(function(m) { | |
m.cb(args); | |
}); | |
}; | |
return { | |
on: p_on, | |
emit: p_emit | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can't remove events atm.