Bug Tracker

Ticket #9316 (closed feature: wontfix)

Opened 3 years ago

Last modified 3 years ago

Make .delegate(null, 'type', fnHandler) behave like .bind('type', fnHandler)

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

Description (last modified by rwaldron) (diff)

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

comment:1 Changed 3 years ago by rwaldron

  • Status changed from new to open
  • Component changed from unfiled to event

This will fall under the pending 1.7 event DRY out proposal

comment:2 Changed 3 years ago by john

  • Keywords 1.7-discuss added

Nominating ticket for 1.7 discussion.

comment:3 Changed 3 years ago by rwaldron

  • Description modified (diff)

+1, Falls under #9393

comment:4 Changed 3 years ago by jaubourg

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

comment:5 Changed 3 years ago by timmywil

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

comment:6 Changed 3 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 3 years ago by rwaldron

  • Description modified (diff)

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

comment:8 Changed 3 years ago by john

  • Description modified (diff)

-1, Ehh, why not just do that logic in your code?

comment:9 Changed 3 years ago by maranomynet

The point is: The feature is already there. Everything seems to work beautifully - but then it breaks down under specific rare (and seemingly random) circumstances.

comment:10 Changed 3 years ago by addyosmani

+1

comment:11 Changed 3 years ago by ajpiano

  • Description modified (diff)

-1, There are a number of cases in jQuery where dynamically building parameters to send to jQuery functions works ALMOST all the time, but due to some kind of data exception, undefined ends up getting passed and jQuery behaves unexpectedly. (undefined as context or the second argument to attr, for instance). Historically, we have not special cased these situations, and I don't think we should start now, especially given the .on enhancement that will handle this problem generically.

comment:12 Changed 3 years ago by scott.gonzalez

  • Description modified (diff)

-1

comment:13 Changed 3 years ago by rwaldron

  • Status changed from open to closed
  • Description modified (diff)
  • Priority changed from undecided to low
  • Milestone changed from 1.next to 1.7
  • Keywords 1.7-discuss removed
  • Resolution set to wontfix

This will be dealt with in #9393

Note: See TracTickets for help on using tickets.