Skip to content

Instantly share code, notes, and snippets.

@sobstel
Created February 8, 2012 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sobstel/1769380 to your computer and use it in GitHub Desktop.
Save sobstel/1769380 to your computer and use it in GitHub Desktop.
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