Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Events
(function () {
var _eventsStack = {};
window.Events = {
emit: function (eventName, message) {
if (!_eventsStack.hasOwnProperty(eventName)) {
_eventsStack[eventName] = [];
}
_eventsStack[eventName].forEach(function (fn) {
fn(message != undefined ? message : {});
});
},
on: function (eventName, fn) {
if (!_eventsStack.hasOwnProperty(eventName)) {
_eventsStack[eventName] = [];
}
_eventsStack[eventName].push(fn);
},
off: function (eventName, fn) {
console.log(_eventsStack)
_eventsStack[eventName].splice(_eventsStack[eventName].indexOf(fn) - 1, 1);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.