Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing behaviors #894

Closed
tmcw opened this issue Nov 13, 2012 · 7 comments
Closed

Removing behaviors #894

tmcw opened this issue Nov 13, 2012 · 7 comments
Milestone

Comments

@tmcw
Copy link
Contributor

tmcw commented Nov 13, 2012

While it's possible to specifically unbind a behavior's handlers, I'm not seeing any clear way to easily remove the behavior itself, in v2 or v3 - if you want an element to be draggable most of the time, but disable the drag handler later.

@mbostock
Copy link
Member

Right, currently you’d need to remove the listeners yourself, which is a bit of a pain:

selection
    .on("mousedown.zoom", null)
    .on("mousewheel.zoom", null)
    .on("mousemove.zoom", null)
    .on("DOMMouseScroll.zoom", null)
    .on("dblclick.zoom", null)
    .on("touchstart.zoom", null)
    .on("touchmove.zoom", null)
    .on("touchend.zoom", null);

I think either we’d need a way to remove an entire namespace, say selection.on("*.zoom", null), or the behavior should expose a method for unbinding from a selection and do the same.

@jfirebaugh
Copy link
Contributor

selection.on("*.zoom", null) would be useful in its own right. I would spell it selection.on(".zoom", null) though, per jQuery's convention.

@mbostock
Copy link
Member

Ah, sure, that sounds fine. Related #880.

And if you say selection.on(".zoom", listener), it overwrites any current listener in the zoom namespace, but has no effect if there are no existing listeners with that namespace?

@mbostock
Copy link
Member

mbostock commented Jul 1, 2013

Not sure if we need another way to unbind a behavior now that selection.on(".zoom", null) has been supported since #1140 in 3.1.

@dannyko
Copy link

dannyko commented Apr 23, 2014

Sorry to ask a possibly dumb question here, but what about the use-case where the user would like to clear all "on" functions that may (or may not) have been bound to any elements over the entire document/DOM, e.g. by some user-generated code? Since the external code might bind the "on" functions to tags like html or body which you don't want to remove, removing them from the DOM would not be a good option as a method for also removing their associated/bound callbacks. Out of blind hope, I tried something naive like d3.on("*", null) and got an error (TypeError: undefined is not a function). Is there a better way, other than explicitly enumerating each type of listener (e.g. click, drag, mouseover, etc.) and doing a ...d3.selectall("*").on(listener_type, null)... type of iteration?

@jasondavies
Copy link
Contributor

Removing all listeners in all namespaces is not supported at the moment, but would be simple enough to add. Removing all namespaced listeners for a particular event type is also not supported, e.g. mousedown.*, but that’s probably not common enough to warrant inclusion.

jasondavies added a commit that referenced this issue Apr 23, 2014
This removes all listeners (namespaced or not) for all elements in a
given selection.

Related discussion in #894.
@jasondavies
Copy link
Contributor

We can discuss further in #1855.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants