Last active
August 29, 2015 14:01
-
-
Save KenneyE/56c27e6036910a3eb89b to your computer and use it in GitHub Desktop.
A custom callback to an event on an object call Fighter
This file contains hidden or 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
_.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