Instantly share code, notes, and snippets.

Embed
What would you like to do?
Observer in JS
/**
* Global beholder for communication between blocks.
*
* If any callback returns true, next callbacks are NOT executed.
*/
var Beholder = function() {
var callbacks = {};
return {
observe: function(id, callback) {
if (typeof(callbacks[id]) == 'undefined') {
callbacks[id] = []
}
callbacks[id].push(callback);
},
notify: function(id, params) {
if (typeof(callbacks[id]) != 'undefined') {
callbacks[id].each(function(callback){
result = callback.call(this, params);
if (result === true) {
throw $break;
}
});
}
}
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment