Created
June 26, 2015 07:30
-
-
Save nashvail/7d42231904a362fb5285 to your computer and use it in GitHub Desktop.
jQuery pub sub API implementation
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
(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