Skip to content

Instantly share code, notes, and snippets.

@hrp
Created January 28, 2012 18:20
Show Gist options
  • Save hrp/1695338 to your computer and use it in GitHub Desktop.
Save hrp/1695338 to your computer and use it in GitHub Desktop.
// from http://darcyclarke.me/development/library-agnostic-pubsub-publish-subscribe/
App = {};
App.cache = {};
App.publish = function(topic, args){
App.cache[topic] && $.each(App.cache[topic], function(){
this.apply($, args || []);
});
};
App.subscribe = function(topic, callback){
if(!App.cache[topic]){
App.cache[topic] = [];
}
App.cache[topic].push(callback);
return [topic, callback];
};
App.unsubscribe = function(handle){
var t = handle[0];
App.cache[t] && $.each(App.cache[t], function(idx){
if(this == handle[1]){
App.cache[t].splice(idx, 1);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment