I hate to be the one that brings this up, but I feel it should be mentioned: Regardless of what your write here, bind() is problematic in the long run:
http://www.ecma-international.org/publications/files/drafts/tc39-2009-025.pdf Jump to the pdf page 128 (the actual document page # is 114) I've recently been working on an elegant feature detection/argument type conditional patch for this issue. Ideally the solution i'm working towards is one that will allow jQuery's current bind() unbind() to work as it does, but also allow the correct Function.prototype.bind() usage Rick On Sat, Sep 5, 2009 at 4:36 AM, Oleg Slobodskoi <oleg...@googlemail.com>wrote: > > Your way is not shorter as this one, and such case, were you need to > bind a lot of different events with different handler to the same > element, is not very often. In the most cases you want to bind > different events to different elements. If you are binding to the same > element a lot of events you can also do all your actions in one > handler by checking the e.type. > > $("#myID").click( function(e) { > // handle click > }) > . mouseover( function(e) { > // handle mouseover > }) > . mouseout : function(e) { > // handle mouseout > }); > > On Sep 3, 10:37 pm, KeeganWatkins <keeganwatk...@gmail.com> wrote: > > First, this conversation started on the learningjquery.com blog > > (http://www.learningjquery.com/2009/06/shorthand-methods-for-unbind). > > > > Ideally, I'd like to see the bind and unbind methods accept an object > > of eventType/handler pairs, such that a single wrapped set could take > > a hash of bindings in a single call. Something along these lines: > > > > (function($) { > > > > // Keep a copy of the old methods > > $.fn._bind = $.fn.bind; > > $.fn._unbind = $.fn.unbind; > > > > // Redefine $().bind() > > $.fn.bind = function( type, data, fn ) { > > // If only a map of handlers was passed... > > return (arguments.length === 1) ? > > > > this.each(function(key, node) { > > // Iterate over the map... > > $.each(type, function(event, handler) { > > event == "unload" ? > > // ... using $.fn.one() for "unload" > events... > > $(this).one(event, handler) : > > // ... and $.event.add() for others > > jQuery.event.add( this, event, handler ); > > }); > > }) : > > > > // Otherwise, use the existing implementation as of 1.3.2, > > // with slight syntactic modifications > > this.each(function(key, node) { > > type == "unload" ? > > // Use $.fn.one() for "unload" events... > > $(this).one(type, fn) : > > // ... and $.event.add() for others > > jQuery.event.add( this, type, fn || data, fn && > data ); > > }); > > > > }; > > > > // Redefine $().unbind() > > $.fn.unbind = function(type, fn) { > > // If only a map of handlers was passed... > > return (arguments.length === 1) > > > > this.each(function(){ > > // Iterate over the map... > > $.each(type, function(event, handler) { > > // ... and unbind each using event.remove() > > jQuery.event.remove( this, event, handler ); > > }); > > }) : > > > > // Otherwise, use the existing implementation as of 1.3.2, > > // copied verbatim > > this.each(function(){ > > jQuery.event.remove( this, type, fn ); > > }); > > > > }; > > })(jQuery); > > > > Usage would be as follows: > > $("#myID").bind({ > > click : function(e) { > > // handle click > > }, > > mouseover : function(e) { > > // handle mouseover > > }, > > mouseout : function(e) { > > // handle mouseout > > } > > > > }); > > > > Or, with named functions (ideal for later use of $().unbind()): > > $("#myID").bind({ > > click : clickHandler, > > mouseover : mouseoverHandler, > > mouseout : mouseoutHandler > > > > }); > > > > function clickHandler(e) { > > // handle click > > > > } > > > > function mouseoverHandler(e) { > > // handle mouseover > > > > } > > > > function mouseoutHandler(e) { > > // handle mouseout > > > > } > > > > With named function references, unbinding multiple events would be > > simple: > > $("#myID").unbind({ > > click : clickHandler, > > mouseover : mouseoverHandler, > > mouseout : mouseoutHandler > > > > }); > > > > I realize there are some potential issues, such as the ability to pass > > custom data to $().bind() is lost when an object of event/handler > > pairs is passed. I personally don't usually use it anyhow, and binding > > events individually will still support custom data (as the > > implementation is the same). > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---