Created
September 5, 2017 16:50
-
-
Save Kolenov/dfc539e9537f41c9a0aaf07207a36e91 to your computer and use it in GitHub Desktop.
pubsub jQ
This file contains hidden or 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
| (function( $ ) { | |
| var o = $( {} ); | |
| $.each({ | |
| trigger: 'publish', | |
| on: 'subscribe', | |
| off: 'unsubscribe' | |
| }, function( key, val ) { | |
| jQuery[val] = function() { | |
| o[key].apply( o, arguments ); | |
| }; | |
| }); | |
| })( jQuery ); | |
| $.getJSON('http://search.twitter.com/search.json?q=dogs&callback=?', function( results) { | |
| $.publish( 'twitter/results', results ); | |
| }); | |
| $.subscribe( 'twitter/results', function( e, results ) { | |
| $('body').html( | |
| $.map( results.results, function( obj, index) { | |
| return '<li>' + obj.text + '</li>'; | |
| }).join('') | |
| ); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Observer/PubSub
Wherefore
This is where the objects in a system may subscribe to other objects and be notified by them when an event of interest occurs.
Howto
Using javascript events