Created — forked from cowboy/HEY-YOU.md

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.

View jquery.ba-tinypubsub.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*!
* jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
 
(function($){
 
// Create a "dummy" jQuery object on which to bind, unbind and trigger event
// handlers. Note that $({}) works in jQuery 1.4.3+, but because .unbind on
// a "plain object" throws errors in older versions of jQuery, an element is
// used here.
var o = $('<b/>');
 
// Subscribe to a topic. Works just like bind, except the passed handler
// is wrapped in a function so that the event object can be stripped out.
// Even though the event object might be useful, it is unnecessary and
// will only complicate things in the future should the user decide to move
// to a non-$.event-based pub/sub implementation.
$.subscribe = function( topic, fn ) {
 
// Call fn, stripping out the 1st argument (the event object).
function wrapper() {
return fn.apply( this, Array.prototype.slice.call( arguments, 1 ) );
}
 
// Add .guid property to function to allow it to be easily unbound. Note
// that $.guid is new in jQuery 1.4+, and $.event.guid was used before.
wrapper.guid = fn.guid = fn.guid || ( $.guid ? $.guid++ : $.event.guid++ );
 
// Bind the handler.
o.bind( topic, wrapper );
};
 
// Unsubscribe from a topic. Works exactly like unbind.
$.unsubscribe = function() {
o.unbind.apply( o, arguments );
};
 
// Publish a topic. Works exactly like trigger.
$.publish = function() {
o.trigger.apply( o, arguments );
};
 
})(jQuery);
View jquery.ba-tinypubsub.js
1 2 3 4 5 6 7 8 9
/*
* jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($){var a=$("<b/>");$.subscribe=function(b,c){function d(){return c.apply(this,Array.prototype.slice.call(arguments,1))}d.guid=c.guid=c.guid||($.guid?$.guid++:$.event.guid++);a.bind(b,d)};$.unsubscribe=function(){a.unbind.apply(a,arguments)};$.publish=function(){a.trigger.apply(a,arguments)}})(jQuery);
Owner

Now smaller and illegible but theoretically faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.