Bug Tracker

Opened 10 years ago

Closed 10 years ago

#6222 closed bug (worksforme)

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:
Blocked by: Blocking:

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 (1)

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

Download all attachments as: .zip

Change History (4)

Changed 10 years ago by roatin.marth@…

Attachment: 6222.html added

Sample breaking test case

comment:1 Changed 10 years ago by john

Resolution: wontfix
Status: newclosed

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 10 years ago by roatin.marth@…

Resolution: wontfix
Status: closedreopened

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 10 years ago by john

Resolution: worksforme
Status: reopenedclosed

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

Note: See TracTickets for help on using tickets.