Skip to content

Instantly share code, notes, and snippets.

@sillero
Last active October 12, 2015 05:08
Show Gist options
  • Save sillero/3975105 to your computer and use it in GitHub Desktop.
Save sillero/3975105 to your computer and use it in GitHub Desktop.
jQuery not-so-tiny Pub/Sub
/* jQuery Not-So-Tiny Pub/Sub - v0.4 - 2013-01-15
* http://sillero.github.com/
* Copyright (c) 2012 "sillero" Gustavo Sillero; Licensed MIT, GPL
*
* UPDATE (2013-01-15): now e (event) gets the folowwing properties
* published (eventName used in $.publish)
* subscribed (eventName used in $.subscribe)
*
* usage:
* $.subscribe('/path1', function(e, response){ console.log('path1',response.data); })
* $.subscribe('/path1/path2', function(e, response){ console.log('subpath2',response.data); })
* $.subscribe('/path1/path2/path3', function(e, response){ console.log('subpath3', response.data); })
* $.publish('/path1/path2/path3', { data: [1,2,3] });
* Console:
* 'subpath3 [1, 2, 3]'
* 'subpath2 [1, 2, 3]'
* 'path1 [1, 2, 3]'
*
* Based on
*
* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
* http://benalman.com/
* Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
;(function($) {
var argsFix = function($args){
$args = Array.prototype.slice.call($args);
$args[0] = $args[0].split('/');
var $newPath = [];
for (var k in $args[0]) {
var v = $args[0][k];
if (v !== undefined && v.length)
$newPath.push(v);
}
$args[0] = $newPath.join('/');
return $args;
};
var o = $({});
$.subscribe = function() {
var $args = argsFix(arguments);
if (!$args[0] || !$args[0].length)
console.error('$.subscribe - event empty');
else {
var $e = 'pubsub';
if ($args[0] !== '*' && $args[0] !== 'all')
$e += '/' + $args[0];
o.on($e, function(){
var $pubArgs = Array.prototype.slice.call(arguments);
$pubArgs[0].subscribed = $args[0];
$pubArgs[0].published = $pubArgs[1].e;
$args[1].apply(this, [$pubArgs[0]].concat($pubArgs[1].res));
});
}
};
$.unsubscribe = function() {
var $args = argsFix(arguments);
o.off.apply(o, $args);
};
$.publish = function() {
var $args = argsFix(arguments),
$eventName = $args[0],
$response = {e: $eventName, res: $args.slice(1) },
$path = $eventName.split('/'),
$pubPath = ['pubsub'].concat($path);
for (var k=0;k<($path.length+1);k++) {
var $pub = [$pubPath.join('/')].concat($response);
o.trigger.apply(o, $pub);
$pubPath.length--;
}
};
}(jQuery));
/* jQuery Not-So-Tiny Pub/Sub - v0.4 - 2013-01-15
* http://sillero.github.com/
* Copyright (c) 2012 "sillero" Gustavo Sillero; Licensed MIT, GPL
*
* UPDATE (2013-01-15): now e (event) gets the folowwing properties
* published (eventName used in $.publish)
* subscribed (eventName used in $.subscribe)
*
* usage:
* $.subscribe('/path1', function(e, response){ console.log('path1',response.data); })
* $.subscribe('/path1/path2', function(e, response){ console.log('subpath2',response.data); })
* $.subscribe('/path1/path2/path3', function(e, response){ console.log('subpath3', response.data); })
* $.publish('/path1/path2/path3', { data: [1,2,3] });
* Console:
* 'subpath3 [1, 2, 3]'
* 'subpath2 [1, 2, 3]'
* 'path1 [1, 2, 3]'
*
* Based on
*
* 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=function(a){a=Array.prototype.slice.call(a),a[0]=a[0].split("/");var b=[];for(var c in a[0]){var d=a[0][c];void 0!==d&&d.length&&b.push(d)}return a[0]=b.join("/"),a},c=a({});a.subscribe=function(){var a=b(arguments);if(a[0]&&a[0].length){var d="pubsub";"*"!==a[0]&&"all"!==a[0]&&(d+="/"+a[0]),c.on(d,function(){var b=Array.prototype.slice.call(arguments);b[0].subscribed=a[0],b[0].published=b[1].e,a[1].apply(this,[b[0]].concat(b[1].res))})}else console.error("$.subscribe - event empty")},a.unsubscribe=function(){var a=b(arguments);c.off.apply(c,a)},a.publish=function(){for(var a=b(arguments),d=a[0],e={e:d,res:a.slice(1)},f=d.split("/"),g=["pubsub"].concat(f),h=0;f.length+1>h;h++){var i=[g.join("/")].concat(e);c.trigger.apply(c,i),g.length--}}})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment