Skip to content

Instantly share code, notes, and snippets.

@davidpadbury
Created May 11, 2011 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidpadbury/966826 to your computer and use it in GitHub Desktop.
Save davidpadbury/966826 to your computer and use it in GitHub Desktop.
Extremely basic pubsub example (please don't use it)
(function(global) {
var subs = {},
slice = Array.prototype.slice;
global.pubsub = {
sub: function(topic, fn, ctx) {
var sub = subs[topic] || (subs[topic] = []);
sub.push({
fn: fn,
ctx: ctx
});
},
pub: function(topic) {
var topicSubs = subs[topic] || [],
args = slice.call(arguments, 1);
for (var i = 0; i < topicSubs.length; i++) {
var sub = topicSubs[i];
sub.fn.apply(sub.ctx, args);
}
}
};
})(window.lib = window.lib || {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment