Bug Tracker

Modify

Ticket #6222 (closed bug: worksforme)

Opened 3 years ago

Last modified 3 years ago

Breaking change in bind() when binding to the same handler multiple times

Reported by: roatin.marth@… Owned by:
Priority: Milestone: 1.4.3
Component: event Version: 1.4.2
Keywords: bind multiple Cc:
Blocking: Blocked by:

Description

Given:

<code>

function foo() { alert('bar') }

$(elem).click(foo); $(elem).click(foo); $(elem).click(foo);

</code>

In jQuery 1.3.2, clicking on elem would cause the alert to show only once, as per the addEventListener standard  https://developer.mozilla.org/en/DOM/element.addEventListener#Multiple_identical_event_listeners:

"If multiple identical EventListeners are registered on the same EventTarget with the same parameters, the duplicate instances are discarded. They do not cause the EventListener to be called twice..."

In jQuery 1.4.2 clicking on elem will cause the alert to show not once, not twice, but thrice.

Attachments

6222.html Download (430 bytes) - added by roatin.marth@… 3 years ago.
Sample breaking test case

Change History

Changed 3 years ago by roatin.marth@…

Sample breaking test case

comment:1 Changed 3 years ago by john

  • Status changed from new to closed
  • Resolution set to wontfix

That was an intentional change in 1.4.2 and was mentioned in the release notes. We made this change because we have multiple ways of binding a handler to an object - a handler with data, a handler with namespaces, a handler with data and namespaces, and even just a plain handler. Considering all these possibilities it ended up making a lot of sense to just allow users to bind events multiple times (especially since they were trying to do so already, in those different contexts, and having serious problems).

comment:2 Changed 3 years ago by roatin.marth@…

  • Status changed from closed to reopened
  • Resolution wontfix deleted

Is this what you're referring to?  http://blog.jquery.com/2010/02/19/jquery-142-released/:

"It’s now possible to bind identical handlers with different data, namespaces, and event types universally."

If so, it is not obvious from that snippet that if you're using plain ol' DOM event binding (ie no event data, no namespaces), 1.4 will break code that previously worked in 1.3.x.

Every other DOM library does what the spec outlines (ie, discard identical handlers). IE's attachEvent is the only event model that screws it up, and now jQuery 1.4 has "normalized" all the previously working browsers to all behave like IE.

Can this be better documented somewhere?

comment:3 Changed 3 years ago by john

  • Status changed from reopened to closed
  • Resolution set to worksforme

I added more detailed information and an example to the bind documentation:  http://api.jquery.com/bind/

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.