Skip to content

Instantly share code, notes, and snippets.

@boazsender
Created May 8, 2011 02:55
Show Gist options
  • Save boazsender/961057 to your computer and use it in GitHub Desktop.
Save boazsender/961057 to your computer and use it in GitHub Desktop.
Implementing what a static jQuery pubsub might look like even though everyone hates the idea.
/*
usage:
$.bind('customEvt', function( event ){
console.log( event );
});
$.trigger('customEvt');
*/
;(function ($) {
$.bind = function( type, data, fn ){
if ( arguments.length === 2 || data === false ) {
fn = data;
data = undefined;
}
jQuery.event.add( document, type, fn, data );
};
$.trigger = jQuery.event.trigger;
})( jQuery );
@boazsender
Copy link
Author

I realize that $.bind and e.data clobber native properties (of function and message event respectively) which is probably bad, but that's another story. If it becomes clear that a built in pub sub is a good idea, then we can work out the semantics later. $.on, or something (which is not very jQuery :( ).

@rwaldron
Copy link

$.bind === Function.prototype.bind; // true get it? its not "just a property" its a fundamental jQuery-will-not-do-it deal breaker.

Additionally, your most recent example is not handling custom data correctly, See: http://jsfiddle.net/rwaldron/jEU3w/

"not very jQuery"? What then, would be "jQuery" enough for you?

As for "overloads", the whole point of the 1.7 events re-write is to stop doing major overloads and create straight forward behaviours that are both backwards compatible and future proof.

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