Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#9959 closed bug (invalid)

$.fn.unbind("eventtype", null) unbinds all handlers with type "eventtype"

Reported by: kpozin@… Owned by: kpozin@…
Priority: low Milestone: None
Component: event Version: 1.6.2
Keywords: Cc:
Blocked by: Blocking:

Description

jQuery.event.remove currently unbinds all handlers matching the event type and namespace if ( !handler ). This means that if null is accidentally passed in, all matching handlers will be unbound.

There should be a distinction between the handler argument being absent (using arguments.length or typeof handler === "undefined") and the handler argument not being a function. If the argument is absent, the intention is to unbind all matching events. If the handler argument is present but not a function, nothing should be done (as if nothing matched its guid).

Change History (6)

comment:1 Changed 8 years ago by danheberden

I'm confused at the use-case for this. Why would anyone "accidentally" pass in null? If nothing is to be unbound, the function should not be called, ya? Plus if i'm after a boolean option to unbind, i'd rather is be falsy instead of someVar ? undefined : null;

comment:2 Changed 8 years ago by dmethvin

Why are you calling jQuery.event.remove directly anyway? It is not a public API. And if you passed in null it sounds like you have a code error, like danheberden said.

comment:3 Changed 8 years ago by dmethvin

Owner: set to kpozin@…
Status: newpending

comment:4 Changed 8 years ago by kpozin@…

Status: pendingnew

Hmm, maybe the fix should be on unbind rather than on remove. Yes, passing in null is probably a code error. But when null is passed as a handler to .bind(), it's handled gracefully (without throwing an exception). For unbinding, I think handling that case gracefully would mean to not unbind all unrelated handlers.

comment:5 Changed 8 years ago by Rick Waldron

Component: unfiledevent
Priority: undecidedlow
Resolution: invalid
Status: newclosed

I'm not sure why we're even debating this. Passing null as a second arg to unbind is incorrect. The second arg has accepted params: handler or false;

comment:6 Changed 8 years ago by dmethvin

Given the reply, I agree this is invalid.

But when null is passed as a handler to .bind(), it's handled gracefully (without throwing an exception).

That is not documented behavior, I wouldn't depend on it to work that way forever.

Note: See TracTickets for help on using tickets.