Bug Tracker

Opened 9 years ago

Last modified 9 years ago

#9316 closed feature

Make .delegate(null, 'type', fnHandler) behave like .bind('type', fnHandler) — at Version 7

Reported by: maranomynet Owned by:
Priority: low Milestone: 1.7
Component: event Version:
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by Rick Waldron)

There are circumstances where it would be incredibly handy if jQuery.fn.delegate would handle the special case of an empty/falsy selector parameter, and interpret it as a direct .bind() request on the collection.

The current behavior in jQuery happens to be almost like that, except that when the collection object is missing a selector property things start failing. I suspect there's some sort of a fallback on .live() going on - and causing those unpredictable results.

Adding this as a feature shouldn't add too much bloat to the core library, and would IMHO adhere to the principle of least surprise.

Change History (7)

comment:1 Changed 9 years ago by Rick Waldron

Component: unfiledevent
Status: newopen

This will fall under the pending 1.7 event DRY out proposal

comment:2 Changed 9 years ago by john

Keywords: 1.7-discuss added

Nominating ticket for 1.7 discussion.

comment:3 Changed 9 years ago by Rick Waldron

Description: modified (diff)

+1, Falls under #9393

comment:4 Changed 9 years ago by jaubourg

-1, I dunno, seems to me like a very edgy case.

comment:5 Changed 9 years ago by timmywil

-1, I'd prefer the other way around, and make .on() an alias for bind.

comment:6 Changed 9 years ago by maranomynet

I'd like to explain the events that lead to my filing this ticket.

I have a plugin that binds events to a container element. The plugin also accepts a target subselector option to limit the events to just certain elements.

My initial assumption was that passing an empty/null selector to .delegate would act just like .bind. So I tried it:

this.delegate( options.targets||'', 'click', handlerFunc );

...and everything worked perfectly just as I had assumed.

Then after a few weeks of happy use things started to randomly fail, it seemed. After a long and confusing debugging session I finally figured out that the cause was a seemingly aribtary lack of .selector property on the collection.

The following is the code I ended up reverting back to:

if ( options.targets ) {
  this.delegate( options.targets, 'click', handlerFunc );
} else {
  this.bind( 'click', handlerFunc );
}

...and it seems a lot less elegant than the original version.

comment:7 Changed 9 years ago by Rick Waldron

Description: modified (diff)

I think this ticket should be closed in favor of #9393

Note: See TracTickets for help on using tickets.