Skip to content

Instantly share code, notes, and snippets.

@fhemberger
Created October 29, 2012 15:57
Show Gist options
  • Save fhemberger/3974360 to your computer and use it in GitHub Desktop.
Save fhemberger/3974360 to your computer and use it in GitHub Desktop.
Pubsub with jQuery $.Callbacks
var topics = {};
$.Topic = function( id ) {
if ( !topics[ id ] ) {
var callbacks = jQuery.Callbacks();
topics[ id ] = {
publish: callbacks.fire,
subscribe: callbacks.add,
unsubscribe: callbacks.remove
};
}
return topics[ id ];
};
$.Topic( "myTopic" ).subscribe( console.log );
// Then in another module far far away
$.Topic( "myTopic" ).publish( "hihihi" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment