Created
April 16, 2012 06:15
-
-
Save SanderSpies/2396654 to your computer and use it in GitHub Desktop.
PubSubManager
This file contains hidden or 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
define([], function () { | |
var PubSubManager = function PubSubManager() { | |
this.subscribers = {}; | |
}; | |
var instance = undefined; | |
PubSubManager.getInstance = function PubSubManager$getInstance() { | |
if (!instance) { | |
instance = new PubSubManager(); | |
} | |
return instance; | |
}; | |
PubSubManager.prototype.publish = function PubSubManager$publish(event, data) { | |
if (this.subscribers[event]) { | |
for (var i = 0, l = this.subscribers[event].length; i < l; i++) { | |
var subscriber = this.subscribers[event][i]; | |
subscriber.apply(subscriber, [data]) | |
} | |
} | |
}; | |
PubSubManager.prototype.subscribe = function PubSubManager$subscribe(event, subscriber) { | |
if (!this.subscribers[event]) { | |
this.subscribers[event] = []; | |
} | |
this.subscribers[event].push(subscriber); | |
}; | |
return PubSubManager.getInstance(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment