jQuery pub/sub in 111 bytes gzipped inspired by @cowboy's Tiny Pub/Sub
Last active
September 29, 2015 19:27
-
-
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
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
<!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> |
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
/* 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)); |
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
/*! 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