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

Dispatch events using selection.dispatch? #41

Closed
mbostock opened this issue May 2, 2016 · 7 comments
Closed

Dispatch events using selection.dispatch? #41

mbostock opened this issue May 2, 2016 · 7 comments
Assignees

Comments

@mbostock
Copy link
Member

mbostock commented May 2, 2016

Rather than listening to the transition object, it would make a lot of sense to use selection.on to listen for transition events, since they happen on a per-element basis anyway. So instead of this:

selection.transition()
    .on("start", function() { console.log("started!"); })
    .style("color", "red");

Something like this:

selection
    .on("d3:transitionstart", function() { console.log("started!"); })
  .transition()
    .style("color", "red");

Or maybe if transition.on is an alias for selection.on:

selection.transition()
    .on("d3:transitionstart", function() { console.log("started!"); })
    .style("color", "red");

This would allow us to remove transition.on and the related machinery, which is great. It might add a little bit of overhead since we’d be dispatching a CustomEvent on every start, end and interrupt. But maybe it’s negligible?

Another potential downside is that you can only (easily) listen to all transition events on a given element, rather than listening to transition events for a particular transition.

This is related to ongoing work in d3-drag, where I need to decide between listening to the drag behavior for events, or having the drag behavior dispatch (custom) drag events using selection.dispatch.

@mbostock mbostock self-assigned this May 2, 2016
@mbostock mbostock changed the title Dispatch transition events using selection.dispatch. Dispatch events using selection.dispatch? May 2, 2016
@mbostock
Copy link
Member Author

mbostock commented May 4, 2016

The primary issue here is that we typically want to listen to events for a specific transition, not for all transitions, and thus we also typically want to remove listeners automatically after the transition ends.

I wonder if transition.on could register a filtered listener that only receives transitions events for the given transition, and that is automatically removed when the transition ends.

@mbostock
Copy link
Member Author

mbostock commented May 6, 2016

Let’s stick with d3-dispatch for events.

@mbostock mbostock closed this as completed May 6, 2016
@Dhanraj073
Copy link

when using selection.on after ..selection.transition() gives an error ...on() is not a function ....but gives correct result if used in reverse order.. Any reason ?

@mbostock
Copy link
Member Author

@Dhanraj073 They mean different things, and it depends on whether you’re using D3 3.x or D3 4.0 alpha.

In D3 4.0, selection.transition returns a transition, and transition.on is different from selection.on, so the order does matter because it means you’re calling different methods. selection.on registers a native browser event listener (such as for mousemove events) while transition.on registers a transition event listener (such as for transition end events).

In D3 3.x, selection.transition returns a transition, and there’s no transition.on method. If you want to listen for transition events, you need to use transition.each (which was renamed to transition.on in D3 4.0 for consistency).

@Dhanraj073
Copy link

@mbostock Got it.

@mbostock
Copy link
Member Author

mbostock commented May 20, 2016

@Dhanraj073 Please use Stack Overflow tag d3.js or the d3-js Google group to ask for help. These issues are for feature requests and bug reports only. Thank you!

Hint: If you’re using D3 4.0 it’s d3.scaleOrdinal, not d3.scale.ordinal.

@curran
Copy link
Contributor

curran commented May 21, 2016

Links for reference

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

3 participants