#9959 closed bug (invalid)
$.fn.unbind("eventtype", null) unbinds all handlers with type "eventtype"
Reported by: | Owned by: | ||
---|---|---|---|
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 12 years ago by
comment:2 Changed 12 years ago by
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 12 years ago by
Owner: | set to [email protected]… |
---|---|
Status: | new → pending |
comment:4 Changed 12 years ago by
Status: | pending → new |
---|
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 12 years ago by
Component: | unfiled → event |
---|---|
Priority: | undecided → low |
Resolution: | → invalid |
Status: | new → closed |
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 12 years ago by
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.
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;