Skip to content

Instantly share code, notes, and snippets.

@nashvail
Created June 26, 2015 07:30
Show Gist options
  • Save nashvail/7d42231904a362fb5285 to your computer and use it in GitHub Desktop.
Save nashvail/7d42231904a362fb5285 to your computer and use it in GitHub Desktop.
jQuery pub sub API implementation
(function ($) {
var o = $( {} ); // to return an instance of jquery that is to pass an empty object literal to jQuery to get a new
$.each({
trigger : 'publish',
on : 'subscribe',
off : 'unsubscribe'
}, function(key, val) {
jQuery[val] = function() {
o[key].apply(o, arguments);
};
});
})(jQuery);
// example usage
$.getJSON(url, function(data) {
$.publish('twitter/results', data);
});
// ...
$.subscribe('twitter/results', function(e, results) {
console.log(results);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment