Skip to content

Instantly share code, notes, and snippets.

@Kolenov
Created September 5, 2017 16:50
Show Gist options
  • Select an option

  • Save Kolenov/dfc539e9537f41c9a0aaf07207a36e91 to your computer and use it in GitHub Desktop.

Select an option

Save Kolenov/dfc539e9537f41c9a0aaf07207a36e91 to your computer and use it in GitHub Desktop.
pubsub jQ
(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('')
);
});
@Kolenov
Copy link
Author

Kolenov commented Sep 5, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment