Created
October 23, 2013 18:22
-
-
Save gipp/7123883 to your computer and use it in GitHub Desktop.
Some super-simple event-emitter functions
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 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