Skip to content

Loading…

Empty default enter() and exit() for all selections (previously undefined) #790

Closed
wants to merge 1 commit into from

2 participants

@ZJONSSON

This allows for better consistancy among selection objects, with enter() exit() chains ignored if no new .data() has been defined for the selection. Should be backwards compatible.

@ZJONSSON ZJONSSON enter() and exit() point to an empty selection by default, not undefi…
…ned. This allows for better consistancy among selection objects, with enter() exit() chains ignored if no new data has been defined for the selection.
e07539f
@mbostock
Owner

d3.select() would return a single-element selection, where the element is null, which is slightly different than say d3.selectAll([]) which returns a zero-element selection.

Also, it would be more performant to define these statically rather than on each selection instance, say:

var d3_selectionEmpty = d3_selection([]);

d3_selectionPrototype.enter = function() {
  return d3_selectionEmpty;
};

d3_selectionPrototype.exit = function() {
  return d3_selectionEmpty;
};

But, I’m not sure it’s a great idea to reuse a singleton empty selection for all default selection.enter and selection.exit methods, since that selection could be modified and then cause unexpected behavior.

At any rate, code that’s trying to access the enter and exit selection on a selection that is not the result of a data-join seems like a bad thing, and I don’t seem a compelling need to make that behavior fail silently with noop selections. I have never needed this behavior in practice since the code that access the enter & exit selections is always immediately preceded by the data join. And that seems like a practice we should encourage.

Thank you, though, for the pull request!

@mbostock mbostock closed this
@ZJONSSON

Yup, I get it. I came up with this request on the basis of a specific pattern I was using (might be considered unconventional). Let's say you have one routine that (perhaps on an interval) refreshes external data and resets a local selection variable as s=d3.select(".elements").data(newdata). On the other hand you might have a GUI interface that changes individual elements of the data through datum. By having default enter() and exit() in the selection object you can use the same code to redraw/replace, independent of the type of selection object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 30, 2012
  1. @ZJONSSON

    enter() and exit() point to an empty selection by default, not undefi…

    ZJONSSON committed
    …ned. This allows for better consistancy among selection objects, with enter() exit() chains ignored if no new data has been defined for the selection.
Showing with 1 addition and 0 deletions.
  1. +1 −0 src/core/selection.js
View
1 src/core/selection.js
@@ -1,5 +1,6 @@
function d3_selection(groups) {
d3_arraySubclass(groups, d3_selectionPrototype);
+ groups.enter = groups.exit = function() { return d3.select(); };
return groups;
}
Something went wrong with that request. Please try again.