Skip to content

Instantly share code, notes, and snippets.

@alexeypegov
Created May 18, 2012 09:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexeypegov/2724275 to your computer and use it in GitHub Desktop.
Save alexeypegov/2724275 to your computer and use it in GitHub Desktop.
Simple Javascript PubSub (observer pattern) AMD-compatible implementation for Loose Coupling
// simple observer (pubsub) implementation
define(["underscore", "base/classex", "util/logging"], function(_, Class, Logging) {
var PubSub = Class.extend({});
PubSub._topics = function() {
if (!window.pubsub) {
window.pubsub = {};
}
return window.pubsub;
};
PubSub.subscribe = function (topic, callback) {
var topics = PubSub._topics();
if (!topics[topic]) {
topics[topic] = [];
}
var pair = [callback];
if (arguments.length == 3) {
// context?
pair.push(arguments[2]);
}
topics[topic].push(pair);
return [topic, callback];
};
PubSub.unsubscribe = function (handle) {
var topic = handle[0];
var topics = PubSub._topics();
topics[topic] && _.each(topics[topic], function (l, i) {
if (l[0] === handle[1]) {
topics[topic].splice(i, 1);
}
});
};
PubSub.publish = function (topic) {
var args = _.map(arguments,function (arg) { return arg; });
args.shift();
var topics = PubSub._topics();
_.each(topics[topic], function (l) {
l[0].apply(l.length == 2 ? l[1] : PubSub, args);
});
};
return PubSub;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment