Skip to content

Instantly share code, notes, and snippets.

@addyosmani
Forked from cowboy/HEY-YOU.md
Created October 27, 2011 10:10
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save addyosmani/1319216 to your computer and use it in GitHub Desktop.
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery 1.7
/*!
* jQuery Tiny Pub/Sub for jQuery 1.7 - v0.1 - 27/10/2011
* Based on @cowboy Ben Alman's REALLY tiny pub/sub.
* 1.7 specific updates by @addyosmani.
* Copyright maintained by @cowboy.
* Dual licensed under the MIT and GPL licenses.
*/
(function($){
// Create a "dummy" jQuery object on which to call on, off and trigger event
// handlers. Although using {} throws errors here in older versions of jQuery
// if you are using 1.7, you won't have this issue.
var o = $({});
// Subscribe to a topic. Works just like on, 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.on( topic, wrapper );
};
// Unsubscribe from a topic.
$.unsubscribe = function() {
o.off.apply( o, arguments );
};
// Publish a topic
$.publish = function() {
o.trigger.apply( o, arguments );
};
})(jQuery);
@addyosmani
Copy link
Author

Note: As jQuery 1.7 pipes all calls to .bind()/.unbind()/.delegate()/.live() etc. through to the rewritten events API (namely .on()/.off()), this is an update to Ben's original solution that directly uses .on() and .off() rather than having an additional layer of abstraction to accessing those methods. That said, the original REALLY tiny pub/sub will still continue to work as the API if of course backwards compatible.

@addyosmani
Copy link
Author

@addyosmani
Copy link
Author

Note 2: This is really just a proof of concept. I would almost always recommend using the original (unless we can show there are significant perf benefits to using .on/.off directly over the (now) abstracted .bind()/.unbind() versions etc).

@elijahmanor
Copy link

@addy: My guess is that the on/off implementation above should be faster when compared to the previous implementation (bind/unbind) using 1.7. It should only be slightly faster because bind/unbind are just one line wrappers around the on/off methods http://o61.go.ly

With that in mind the on/off methods in 1.7 could very well be slower than the bind/unbind methods in 1.6.4. That would be an interesting comparison. I hope the answer is better performance in 1.7 & not worse ;)

@albanx
Copy link

albanx commented Mar 15, 2014

Hi Andy
Excellent pattern. BUt I am having some problems on the context of the callback function. Example:
$.subscribe("dashboard.updateData", this.updateData ); //this--> currenct object

then :
updateData: function(){
this--> here is the event object, but here I will need the original object
}

Is there a way to make callbacks fire in scope of originating object instead ?

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