Created
May 11, 2011 16:36
-
-
Save davidpadbury/966826 to your computer and use it in GitHub Desktop.
Extremely basic pubsub example (please don't use it)
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
(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