Bug Tracker

Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#7520 closed bug (worksforme)

$(document).unbind('click') seems to clear live click callbacks

Reported by: art.orlov Owned by:
Priority: low Milestone:
Component: event Version: 1.4.4
Keywords: neededdocs Cc:
Blocked by: Blocking:

Description

It seems that doing $(document).unbind('click') removes not only all click handlers attached to document node but also live click handlers like: $('a[data-method]:not([data-remote])').live('click', ...)

The same stuff with $('body').delegate() because it uses the same live technique.

It seems clear that $(document).unbind('click') should only clear callbacks attached using bind function. And die is designed for live callbacks.

Change History (11)

comment:1 Changed 9 years ago by jitter

possible test case

comment:2 Changed 9 years ago by boushley

Dang jitter beat me to it! Here's my fiddle anyways http://jsfiddle.net/boushley/y5fVv/

comment:3 Changed 9 years ago by jitter

Well I like yours better anyway :D

comment:4 Changed 9 years ago by Neeraj Singh

Not sure if it is valid but I attempted to create a failing test. https://github.com/neerajdotname/jquery/commit/9cce81f0c19ed3506f0d955716f280b2e60d2fcc

comment:5 in reply to:  4 Changed 9 years ago by art.orlov

Replying to Neeraj Singh:

Not sure if it is valid but I attempted to create a failing test. https://github.com/neerajdotname/jquery/commit/9cce81f0c19ed3506f0d955716f280b2e60d2fcc

Seems to be absolutely correct testcase, that's what we did in our code. Thanks for writing it.

Going spend time next time on writing testcases instead of text description :)

comment:6 Changed 9 years ago by Rick Waldron

Component: unfiledevent
Keywords: needsreview added
Milestone: 1.5
Priority: undecidedlow
Status: newopen

Confirmed - but it the behaviour is consistent all the way back to 1.2.6

http://jsfiddle.net/rwaldron/y5fVv/4/

This is a little clearer, and further reduced. The test result is laid out.

comment:7 Changed 9 years ago by dmethvin

Given the implementation this makes sense; there is a special "live" event attached to the delegate elements (document for the .live() case but any element for .delegate()) and if you unbind all events from an element it, well, unbinds ALL events.

Should we just document it works this way, or somehow segregate the events better? I'd prefer to see it documented because it seems like a lot of work to keep them apart. Long term I think there could be some benefit to unifying our event APIs similar to what Prototype did with .on("event", "selector", data, fn) and if we start treating them differently that will make less sense.

comment:8 Changed 9 years ago by art.orlov

I see that such behavior is not transparent to user. And i guess most of them don't know how .live() event handler works. I'd prefer the way of separating delegated and direct event handlers. Like the way native event handlers (attached via .addEventListener etc.) are separated from ones handled by jQuery.

comment:9 Changed 9 years ago by john

Keywords: needsdocs added; needsreview removed
Resolution: worksforme
Status: openclosed

I think that this is "OK" (in that it's the expected result - delegate and live both are attaching click handlers to the document). I don't think we should try to obscure this, that might end up creating unexpected results. I'm going to flag this as "needsdocs" so that we can go back later and improve the documentation here.

comment:10 Changed 9 years ago by addyosmani

Initial docs update: http://api.jquery.com/unbind/. Could be improved.

comment:11 Changed 9 years ago by addyosmani

Keywords: neededdocs added; needsdocs removed
Note: See TracTickets for help on using tickets.