Created
March 12, 2017 00:05
-
-
Save Kolenov/9cfea7780cf23b2e1a089a2361e83e20 to your computer and use it in GitHub Desktop.
jQuery Observer Pattern
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 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