Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bind an event handler and fire it immediately
/*!
* bindAndTrigger - v0.1 - 04/30/2010
* http://benalman.com/
*
* http://jsfiddle.net/cowboy/fJnA2/
*/
(function($,undefined){
$.fn.bindAndTrigger = function( all, type, data, callback ) {
if ( typeof all !== 'boolean' ) {
callback = data;
data = type;
type = all;
all = undefined;
}
var first = type.split(' ')[0],
fn = callback || data;
return this
.bind( type, data, callback )
.each(function(){
var event = $.Event( first );
fn.call( event.target = this, event );
return !!all;
});
};
})(jQuery);
@westonruter

This comment has been minimized.

Copy link

@westonruter westonruter commented Apr 30, 2010

I forked this at http://gist.github.com/385402

Variable function arguments are pretty painful in JavaScript now, aren't they. I'm playing around with some JavaScript 1.7 destructuring assignments.

@westonruter

This comment has been minimized.

Copy link

@westonruter westonruter commented Apr 30, 2010

Instead of the all argument, what about adding support for event.stopImmediatePropagation() being called in the handler? In the each(…) loop you could then return !event.isImmediatePropagationStopped(); instead of return !!all;.

@cowboy

This comment has been minimized.

Copy link
Owner Author

@cowboy cowboy commented Apr 30, 2010

Using arguments is generally slower, and almost always more code.. event.stopImmediatePropagation() also seems cool, but again, more code (and maybe more complex for the end user).

@westonruter

This comment has been minimized.

Copy link

@westonruter westonruter commented Apr 30, 2010

While using arguments is slower and uses more code, don't you think it's more readable? Too bad we can't pass named arguments like Python's **kwargs without relying on passing in a single args object:

function demo(all, type, data, callback){
    …
}

Which could then be called by traditionally passing in positional arguments:

demo( true, 'click', null, func );

Or by passing in named arguments in any order:

demo( callback:func, data:null, all:true, type:'click' );

This could easily be supported by the runtime by just positioning the named arguments and potentially injecting undefined. Actually, I think this is better than Python's **kwargs for a finite set of arguments since named arguments in Python always get assigned to the kwargs dictionary as far as I know.

@cowboy

This comment has been minimized.

Copy link
Owner Author

@cowboy cowboy commented Apr 30, 2010

I used to have a lot of fancy arguments stuff in jQuery Object Utils (predecessor to jQuery getObject) but it ended up being a bit gross. In this particular example, it actually simplified things greatly (imo) to use explicit vars.

One or two if statements to shift args around might not feel beautiful, but it minifies really small and executes super-fast.

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