Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
* Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
(function($) {
var o = $({});
$.subscribe = function() {
o.on.apply(o, arguments);
$.unsubscribe = function() {, arguments);
$.publish = function() {
o.trigger.apply(o, arguments);
/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
* 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(){,arguments)},a.publish=function(){b.trigger.apply(b,arguments)}})(jQuery)
Copy link

mediafreakch commented Jul 10, 2014

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?

Copy link

gmanish commented Dec 4, 2015

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.

Copy link

shshaw commented Apr 14, 2016

@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 />').

Copy link

Kiodaddy commented Jun 20, 2017

I am telling you this is working really good.

Copy link

ahmed-musallam commented Aug 31, 2017

Copy link

lanikane commented Aug 16, 2022

Your article is very helpful for me. I am very impressed with your article. It gives me a lot of interesting information about quordle moviedle . Thank you very much.

Copy link

Rosendamans commented Aug 22, 2022

I suppose I should pose that issue on a Zepto forum, but I thought someone might have a workaround here. awnings near me Toms River NJ

Copy link

joanadavidson commented Sep 15, 2022

Zepto only makes use of the DOM-based event system so there's no way to get a real empty element to work as the global pub/sub-object. We might as well use ("").

Joana | peeling wallpaper

Copy link

biano22 commented Sep 29, 2022

Thank you very much for your sharing, it has helped me in my great work, I have reduced my working time a lot because of you. I have coded some games like quordle , temple run .. if you are interested please connect with me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment