Skip to content

Instantly share code, notes, and snippets.

@dciccale
Last active September 29, 2015 19:27
Show Gist options
  • Save dciccale/1653547 to your computer and use it in GitHub Desktop.
Save dciccale/1653547 to your computer and use it in GitHub Desktop.
jQuery xxspubsub in 111 bytes gzipped inspired by @cowboy's Tiny Pub/Sub

jQuery XXSPubSub

jQuery pub/sub in 111 bytes gzipped inspired by @cowboy's Tiny Pub/Sub

<!doctype html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script>
(function(d,a){a=d({});d.subscribe=function(b,c){a.on.call(a,b,c)};d.unsubscribe=function(b,c){a.off.call(a,b,c)};d.publish=function(b,c){a.trigger.call(a,b,c)}})(jQuery);
</script>
<script>
var handler = function (event, name) {
alert(name);
};
$.subscribe('sayName', handler);
$.publish('sayName', 'denis'); // alert('denis');
$.unsubscribe('sayName', handler);
$.publish('sayName', 'denis'); // nothing hapens
</script>
/* jQuery XXS Pub/Sub
* Copyright (c) 2013 Denis Ciccale (@tdecs)
* Released under the MIT license
*/
(function ($, o) {
o = $({});
$.subscribe = function(e, h) {
o.on.call(o, e, h);
};
$.unsubscribe = function(e, h) {
o.off.call(o, e, h);
};
$.publish = function(e, h) {
o.trigger.call(o, e, h);
};
}(jQuery));
/*! jQuery xxspubsub | @tdecs | under the MIT license */
(function(d,a){a=d({});d.subscribe=function(b,c){a.on.call(a,b,c)};d.unsubscribe=function(b,c){a.off.call(a,b,c)};d.publish=function(b,c){a.trigger.call(a,b,c)}})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment