Skip to content

Instantly share code, notes, and snippets.

@JordanForeman
Created March 25, 2015 16:20
Show Gist options
  • Select an option

  • Save JordanForeman/d14f7657d95ba66f764f to your computer and use it in GitHub Desktop.

Select an option

Save JordanForeman/d14f7657d95ba66f764f to your computer and use it in GitHub Desktop.
Basic Custom Events
EventDispatcher = {
events: {},
on: function(event, callback) {
var handlers = this.events[event] || [];
handlers.push(callback);
this.events[event] = handlers;
},
trigger: function(event, data) {
var handlers = this.events[event];
if (!handlers || handlers.length < 1)
return;
[].forEach.call(handlers, function(handler){
handler(data);
});
}
};
function initializeListeners() {
EventDispatcher.on('fire', fire); // fire.bind(this) -- if necessary
}
function fire(x) {
console.log(x);
}
function thingHappened(thing) {
EventDispatcher.trigger('fire', thing);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment