Skip to content

Instantly share code, notes, and snippets.

@Kolenov
Created March 12, 2017 00:05
Show Gist options
  • Save Kolenov/9cfea7780cf23b2e1a089a2361e83e20 to your computer and use it in GitHub Desktop.
Save Kolenov/9cfea7780cf23b2e1a089a2361e83e20 to your computer and use it in GitHub Desktop.
jQuery Observer Pattern
var o = $( {} );
$.subscribe = o.on.bind(o);
$.unsubscribe = o.off.bind(o);
$.publish = o.trigger.bind(o);
// Usage
document.on( 'tweetsReceived', function(tweets) {
//perform some actions, then fire an event
$.publish('tweetsShow', tweets);
});
//We can subscribe to this event and then fire our own event.
$.subscribe( 'tweetsShow', function() {
//display the tweets somehow
//
//publish an action after they are shown.
$.publish('tweetsDisplayed');
});
$.subscribe('tweetsDisplayed', function() {
//
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment