Created
July 30, 2009 17:47
-
-
Save GCheung55/158806 to your computer and use it in GitHub Desktop.
MooPubSubBroker
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
/* | |
* 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