jQuery Tinier Pubsub
Testing different implementations of Ben Alman's tinypubsub plugin. See gist and fork-history at: https://gist.github.com/zaus/4756518
by drzaus
HTML
<pre id="output"></pre>
JavaScript
//#region ------------------ style 1 -------------------
/* jQuery Tinier Pub/Sub - v0.9 - @see https://gist.github.com/zaus/4756518 */
(function($) {
// "topic" holder
var o = $({});
// attach each alias method
$.each({on:0,off:0,"do":'trigger'}, function(alias,method) {
$[alias] = function(topic, callbackOrArgs) {
o[method || alias].apply(o, arguments);
}
});
}(jQuery));
//#endregion ------------------ style 1 -------------------
//#region ------------------ style 2 -------------------
(function($) {
// "topic" holder
var o = $({});
// plugin - arbitrary subnamespace for example
$.pbsb = {};
// attach each alias method
$.each(['on','off','trigger'], function(i,method) {
$.pbsb[method] = function(topic, callbackOrArgs) {
o[method].apply(o, arguments);
}
});
}(jQuery));
//#endregion ------------------ style 2 -------------------
//#region ------------------ util -------------------
_log = function() {
_log.$output.innerHTML += "\n" + Array.prototype.slice.call(arguments, 0).join(", ");
}
_log.$output = document.getElementById('output');
//#endregion ------------------ util -------------------
//#region ------------------ test -------------------
$.on('custom/action1', function(e, first, second) {
$.do('custom/all', [first, second, e.type]);
_log('specific', e.type, first, second);
});
$.on('custom/action2', function(e, first, second, third) {
$.do('custom/all', [first, second, third, e.type]);
_log('specific', e.type, first, second, third);
});
$.on('custom/all', function(e, args) {
args = Array.prototype.slice.call(arguments, 1);
_log('global', e.type, args);
});
$.do('custom/action1', ['1st', '2nd', '3rd']);
$.do('custom/action2', ['1st', '2nd', '3rd']);
$.do('custom/action1', ['1st', '2nd', '3rd']);
_log('--------------alternatestyle------------------');
$.pbsb.on('pbsb/action1', function(e,...