Skip to content

Instantly share code, notes, and snippets.

@gipp
Created October 23, 2013 18:22
Show Gist options
  • Save gipp/7123883 to your computer and use it in GitHub Desktop.
Save gipp/7123883 to your computer and use it in GitHub Desktop.
Some super-simple event-emitter functions
function createEmitter(object) {
// a place to hold callbacks for event subscribers
object._eventStore = {};
object.on = function(eventName, fn) {
if (!this._eventStore[eventName]) {
this._eventStore[eventName] = [];
}
this._eventStore[eventName].push(fn);
};
object.off = function(eventName) {
this._eventStore[eventName] = null;
};
object.trigger = function(eventName) {
if (!this._eventStore[eventName]) {
return;
}
this._eventStore[eventName].forEach(function (cb) {
cb();
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment