Skip to content

Instantly share code, notes, and snippets.

@kavanagh
Created May 14, 2013 08:41
Show Gist options
  • Save kavanagh/5574552 to your computer and use it in GitHub Desktop.
Save kavanagh/5574552 to your computer and use it in GitHub Desktop.
Very simple topic subscription.
var a = topic();
a.add(function(){
console.log('a', arguments);
});
a.fire('message for a'); // should be "a", ["message for a"]
// named topic
var b = topic('b');
b.add(function(){
console.log('b', arguments);
});
b.fire('message for b'); // should be "b", ["message for b"]
var c = topic('b');
console.log('b and c are the same', b === c);
c.fire('message for c'); // should be "b", ["message for c"]
var topic = (function($){
var t = {};
return function(name) {
return t[name] = t[name] || $.Callbacks();
}
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment