Created
October 29, 2012 15:57
-
-
Save fhemberger/3974360 to your computer and use it in GitHub Desktop.
Pubsub with jQuery $.Callbacks
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
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