Skip to content

Instantly share code, notes, and snippets.

@GCheung55
Created July 30, 2009 17:47
Show Gist options
  • Save GCheung55/158806 to your computer and use it in GitHub Desktop.
Save GCheung55/158806 to your computer and use it in GitHub Desktop.
MooPubSubBroker
/*
* Converted from: http://blog.narcvs.com/?p=43
*/
var MooPubSubBroker = new Class({
Implements: [Options, Events],
options:{},
initialize: function(args, options){
this.setOptions(options);
this.subscribers = {};
var i = args.length;
while(i--){
this.subscribers[args[i]] = [];
}
},
publish: function(signal, args){
console.log(arguments);
for (var i=0; i < this.subscribers[signal].length; i++) {
(this.subscribers[signal][i])(args);
}
},
subscribe: function(signal, scope, handlerName){
var curryArray = Array.prototype.slice.call(arguments,3);
this.subscribers[signal].push(function(){
var normalizedArgs = Array.prototype.slice.call(arguments,0);
scope[handlerName].apply((scope || window), curryArray.concat(normalizedArgs));
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment