This is now an actual repo:
Last active
October 25, 2024 15:29
-
-
Save cowboy/661855 to your computer and use it in GitHub Desktop.
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for 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 Tiny Pub/Sub - v0.7 - 10/27/2011 | |
* http://benalman.com/ | |
* Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */ | |
(function($) { | |
var o = $({}); | |
$.subscribe = function() { | |
o.on.apply(o, arguments); | |
}; | |
$.unsubscribe = function() { | |
o.off.apply(o, arguments); | |
}; | |
$.publish = function() { | |
o.trigger.apply(o, arguments); | |
}; | |
}(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 Tiny Pub/Sub - v0.7 - 10/27/2011 | |
* http://benalman.com/ | |
* Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */ | |
(function(a){var b=a({});a.subscribe=function(){b.on.apply(b,arguments)},a.unsubscribe=function(){b.off.apply(b,arguments)},a.publish=function(){b.trigger.apply(b,arguments)}})(jQuery) |
I know some javascript and little of jQuery (been playing with Ember, just for fun). I understand the on
off
methods, but I do not understand what var o = $({});
does. Can anyone please explain?
As always, google doesn't consider these braces and the $ in its search results.
@gmanish var o = $({})
simply creates a jQuery collection with an empty object {}
that becomes the recipient of all the event triggers. As other comments show, the recipient could be anything like $('<b />')
.
I am telling you this is working really good.
Made a Typescript version of this here: https://gist.github.com/ahmed-musallam/d12a71b8a3ec8cebf52c12f81f3da154
@cowboy this gist is getting spammed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How would you add support for wildcards in the topic name? Does it even make sense as jQuery doesn't support wildcards for event names? Or is using a standalone pub/sub library the better approach?