This is now an actual repo:
/* 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)); |
/* 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) |
Even smaller just for sillyness
var o=$({}),s='subscribe';
If we're talking about relative sizes, why use "publish/subscribe" nomenclature? Just stick with "on/off/trigger" (or my preferred "on/off/do").
UPDATE I just found out why the "pros" stick to trigger
instead of do
(boo <IE9).
Example: https://gist.github.com/zaus/4756518
/* jQuery Tinier Pub/Sub - v0.9b - "on/off/do version" - 2013-02-11
* original by http://benalman.com/ 10/27/2011
* Original Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
(function($) {
// "topic" holder
var o = $({});
// attach each alias method
$.each({on:0,off:0,"go":'trigger'}, function(alias,method) {
$[alias] = function(topic, callbackOrArgs) {
o[method || alias].apply(o, arguments);
}
});
}(jQuery));
@connected - what's the default event you're trying to prevent? isn't this limited to the arbitrary hidden topic var o
? just curious.
@kodi thanks for the requirejs module implementation!
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?
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
I will try that code. Thank you. https://cincinnati-seo.com/
@cowboy this gist is getting spammed
Thanks for letting us know! Graffiti Removal Northwest Arkansas
I have only one concern/suggestion to this relatively simple PubSub - it doesn't take into account published events that have already happened (in the past). Why is this important? Assume for a second that I want to subscribe to an event that had already happened, or I dont know that it happened, but still want my new subscriber to be triggered with the last-published values? My suggestion is to add something like this:
(function($) {
var o = $({}), pastEvents = {};
$.subscribe = function() {
var type = arguments.slice(0, 1)[0],
handler = arguments.slice(-1)[0];
//Fire your subscribe handler if event has already happened
if(type in pastEvents) {
pastEvents[type].done(function() {
handler.apply(o, arguments);
});
}
//Subscribe to future events as well
o.on.apply(o, arguments);
};
$.unsubscribe = function() {
o.off.apply(o, arguments);
};
$.publish = function() {
var type = arguments.slice(0, 1)[0],
data = arguments.slice(1)[0];
//Preserve data for future subscribers to this event
pastEvents[type] = $.Deferred().resolve(data).promise();
o.trigger.apply(o, arguments);
};
}(jQuery));