nashvail
6/26/2015 - 7:30 AM

jQuery pub sub API implementation

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);
});