Skip to content

Instantly share code, notes, and snippets.

@KenneyE
Last active August 29, 2015 14:01
Show Gist options
  • Save KenneyE/56c27e6036910a3eb89b to your computer and use it in GitHub Desktop.
Save KenneyE/56c27e6036910a3eb89b to your computer and use it in GitHub Desktop.
A custom callback to an event on an object call Fighter
_.extend(Fighter, {
all: [],
events: {},
on: function (eventTitle, callback) {
event[eventTitle] = this.events[eventTitle] || [];
this.events[eventTitle].push(callback);
},
trigger: function (eventTitle){
var callbacks = this.events[eventTitle];
var arg = arguments[1];
callbacks = callbacks || [];
callbacks.forEach(function (callback){
callback(arg);
});
},
//... other stuff....
}
//
Fighter.trigger('enterTheDojo') // Does nothing
Fighter.on('enterTheDojo', function () { askForMoney }); // Establishes event listener
Fighter.trigger('enterTheDojo') // calls `askForMoney`
Fighter.on('enterTheDojo', funciton () { impartWisdom }); // Establishes event listener
Fighter.trigger('enterTheDojo') // call `askForMoney` and then `impartWisdom`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment