Created
May 9, 2017 15:06
-
-
Save joaoneto/a6c783077a4e8dfbb2affb0924e80d4b to your computer and use it in GitHub Desktop.
Custom Events
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
(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