Skip to content

Instantly share code, notes, and snippets.

@SanderSpies
Created April 16, 2012 06:15
Show Gist options
  • Save SanderSpies/2396654 to your computer and use it in GitHub Desktop.
Save SanderSpies/2396654 to your computer and use it in GitHub Desktop.
PubSubManager
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