Created
January 3, 2012 05:05
-
-
Save jarek-foksa/1553576 to your computer and use it in GitHub Desktop.
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
global.events = {}; | |
// | |
// Usage: | |
// bind('editButton->clicked', function(arg) { | |
// doStuff(arg); | |
// }); | |
// | |
global.bind = function(eventName, callback) { | |
var callbacks = global.events[eventName] || (global.events[eventName] = []); | |
callbacks.push(callback); | |
} | |
// | |
// Usage: | |
// trigger('editButton->clicked', true); | |
// | |
global.trigger = function(eventName) { | |
if (debug) { | |
console.log("Triggered event: '" + eventName + "'"); | |
} | |
var that = this; // scoping | |
var args = arguments; // scoping | |
var callbacks = global.events[eventName]; | |
if (Array.isArray(callbacks)) { | |
callbacks.forEach( function(callback) { | |
callback.apply(global, Array.prototype.slice.call(args, 1)); | |
}); | |
} | |
} | |
global.unbind = function(eventName, callback) { | |
var callbacks = global.events[eventName]; | |
if (Array.isArray(callbacks)) { | |
callbacks.forEach( function(item, index) { | |
if (callback.toString() === item.toString()) { | |
callbacks.splice(index, 1); | |
return; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment