You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Usually, when the select operator is run, each newly-selected node inherits its data from the corresponding node in the original selection. However, this was recently changed so that if the new selection already hd data, it would not inherit data from the original:
if (subnode && !subnode.__data__) subnode.__data__ = node.__data__;
However, I'm not sure this is what we want.
First, what if the data is an array of numbers or booleans (or null, rather than undefined)? This could be very confusing in terms of some of the newly-selected nodes inheriting data and some of them not.
Second, the above implementation prevents the new selection from inheriting the data, even if the original selection has data defined. Perhaps we should check if the data was set explicitly on the original selection, so that it can override what's stored in the DOM's __data__?
I feel as though the fix is more confusing than the original implementation. It is also strange that select modifies __data__ , while selectAll does not.
Fix 1
It seems like in most cases, if a child node already has data, you would not want it to inherit its parent's data. To fix the boolean/null problem, we could do:
if (subnode && !("__data__" in subnode)) subnode.__data__ = node.__data__;
Fix 2
A second alternative would be to consider the 'freshest' data; whichever data is newest should win. __data__ could be augmented with a global counter that tracks when the data was set. If both the subnode and node have data, the data with a higher counter would win. This seems overly complex, and I can't think of a good example of when this scenario arises.
Fix 3
To get rid of any ambiguities, I think it is cleaner to never inherit data from the parent. select is then non-volatile. In the cases that you do want to use the parent data, you can use each, to create a closure. For example, instead of:
Activity
mbostock commentedon Nov 10, 2010
Fixed. Changed to
Added the zoomable pack layout from the SVG Open 2011 keynote:
saifelse commentedon Sep 21, 2013
I think this is still problematic. The code snippet found here: https://github.com/mbostock/d3/wiki/Selections#wiki-data has nested data. In trying to call
selecton a single td, its data (a single number) gets overwritten by its parent td's data (an array of numbers). My workaround for now, has been to doselectAlland thenfilterto reduce the size of the selection to the element I care about. The issue is demonstrated in this fiddle: http://jsfiddle.net/FUJa6/1/I feel as though the fix is more confusing than the original implementation. It is also strange that
selectmodifies__data__, whileselectAlldoes not.Fix 1
It seems like in most cases, if a child node already has data, you would not want it to inherit its parent's data. To fix the boolean/null problem, we could do:
Fix 2
A second alternative would be to consider the 'freshest' data; whichever data is newest should win.
__data__could be augmented with a global counter that tracks when the data was set. If both the subnode and node have data, the data with a higher counter would win. This seems overly complex, and I can't think of a good example of when this scenario arises.Fix 3
To get rid of any ambiguities, I think it is cleaner to never inherit data from the parent.
selectis then non-volatile. In the cases that you do want to use the parent data, you can useeach, to create a closure. For example, instead of:you would do: