@Ariel - Thanks! I took a look at the new accept() function, that's a
great idea. When I began exploring this addition, I started with the
implementations for $.fn.css() and $.fn.attr() , so having the logic
that handles variable argument lists in a single place should make it
easier to reuse and understand.

@Karl - Thanks for your feedback!

>> Even without the namespaces, we don't need to "chain the calls to
 >> unbind()," since both bind and unbind can take space-delimited
events
>> as the first argument:

>> $("#myID").unbind('click mouseover mouseout');

That is an excellent point, and one that I overlooked. There is a key
distinction though, in terms of how I would expect to use this new
feature: although (as you've pointed out) we can avoid chaining calls
to unbind by passing a space-delimited list of event types, this would
cause all handlers of the same type to be unbound. One of the goals I
had was to allow more granular control, while still supporting the
existing implementation. If one were interested in removing specific
handlers of a certain type, but not all handlers of said type, then
chaining calls to unbind() (passing both the specific type and
handler) would be the only solution (as far as I can tell). My
comments about "cleaning up after one's self" were largely in relation
to a single handler's removal, independent of other handlers of the
same type.

>> This is what namespaced events
>> are for. If you change Oleg's example just a bit ...

>> $("#myID").bind('click.mynamespace', function(e) {
>>   // handle click
>> })

>> .bind('mouseover.mynamespace', function(e) {
>>    // handle mouseover
>> })

>> .bind('mouseout.mynamespace', function(e) {
>>    // handle mouseout

>> });

>> ... you can unbind this click handler without unbinding other click
>> handlers by doing this:

>> $("#myID").unbind('click.mynamespace');

That is another great point, and in many ways it addresses my desired
use of unbind(). I wonder though, if it is ideal to use a custom event
(or events) for the sole purpose of the added granularity (i.e. the
ability to reference a specific handler at a later point in time). I'm
not advocating against using custom events (for this purpose or any
other), just noting that if the same functionality can be implemented
in other ways, I feel an obligation to at least investigate :) .

On Sep 9, 4:19 pm, Ariel Flesler <afles...@gmail.com> wrote:
> Just added a generic function that should aid the implementation of
> this feature:
>  http://dev.jquery.com/ticket/5189
>
> Also added a ticket for this feature request (plus a couple more
> things)
>  http://dev.jquery.com/ticket/5190
>
> --
> Ariel Flesler
>
> On Sep 3, 5: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
-~----------~----~----~----~------~----~------~--~---

Reply via email to