question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

selection.select and data inheritance.

See original GitHub issue

I’m hesitant to open this as an issue because I know the behaviour of having selection.select inherit data from corresponding nodes in the parent selection is totally deliberate. But I think it’s at least worth discussing so here goes.

This side effect of select is something that confused me when I was first starting out with d3. And I regularly find myself explaining the behaviour to colleagues who are surprised by it. These explanations never seems to stop at the “what happens” but invariably continue into queries of “why would it do that”? I take this repeating pattern as a red flag that there is a rough edge in the API.

I think the confusion stems from 2 points:

  1. The term select has strong connotations of being read-only from popular usage in SQL and other query languages. This seems to run pretty deep for people (myself included).
  2. All other instances of select and selectAll in d3 besides selection.select are read only.

I understand that changing this would be a pretty severe backward compatibility break, but just as a thought experiment, do you see any other issues with the following?:

  • Make all select/selectAll operations to be read only only.
  • Introduce selection.update(selector) to handle the update workflow where you truly do want to propagate data across nodes. This would essentially work just like selection.select does today.

It’s maybe not as seamless but there is something nice (to me) about having mutating operations be explicit even if it means sacrificing some convenience.

BTW, I posted a StackOverflow question a little while ago, but that was more targeted at techniques for working around practical issues we were hitting. It adds some relevant context to this topic, though, so here’s a link.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mbostockcommented, Aug 9, 2013

Not a great answer, but one way you can prevent data inheritance is to have an intermediate node without bound data.

var intermediary = selection.append("div")
    .datum(function() { return null; });

Then, any select from the intermediary wouldn’t propagate data from the parent selection. But of course, the intermediate node in the DOM is somewhat unfortunate.

0reactions
magjaccommented, Nov 19, 2017

In the d3-graphviz library there is never a 1-to-1 relationship between parents and children and it is never the case that the parent and child are bound to the same data. Thus the propagation of data is never desired.

Since the application where I use d3-graphviz frequently wants to access nodes and edges of the generated graph without affecting bound data, I’ve added an extension to d3-selection, selection.selectWithoutDataPropagation which does d3.select(this.node().querySelector(name))

I also added a note to the library documentation to avoid the use of selection.select when building applications with d3-graphviz in order to avoid propagating incorrect data by mistake.

Would you we willing to accept a PR to d3-selection adding this function? If so, would you prefer a different name, e.g selectPure?

Even if this function is simple enough to implement outside d3-selection, its pure existence would further highlight that selection.select does more than just pure selection.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preventing unwanted data inheritance with selection.select
In d3, selection.select has the side-effect of inheriting data from the parent nodes in the original selection. This is desirable in situations ...
Read more >
Cascade, specificity, and inheritance - Learn web development
Specificity is basically a measure of how specific a selector's selection will be: An element selector is less specific; it will select all ......
Read more >
Defaults, classes, and inheritance - IBM
On the Entry Type Selection panel, select 6 APL. Enter new DB2_CLASS to create a new APL entry type with an Object Type...
Read more >
2.3 Inheritance in SQL Object Types - Oracle Help Center
SQL object inheritance is based on a family tree of object types that forms a ... This process of selecting a method implementation...
Read more >
Selecting Data from a Table Inheritance Hierarchy
Direct Transact-SQL Select statements that successfully read from inheritance tables in one release might fail in the next release. Diagram of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found