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

selection.clone? #149

Closed
mbostock opened this issue Oct 26, 2017 · 2 comments
Closed

selection.clone? #149

mbostock opened this issue Oct 26, 2017 · 2 comments

Comments

@mbostock
Copy link
Member

As seen here: https://bl.ocks.org/mbostock/7f5f22524bd1d824dd53c535eda0187f

function selection_clone() {
  return this.parentNode.insertBefore(this.cloneNode(true), this.nextSibling);
}

d3.selection.prototype.clone = function() {
  return this.select(selection_clone);
};

Questions:

  1. Should you be allowed to do shallow clone? (Maybe?)

  2. Should you be allowed to specify where to insert the cloned node, e.g., as the last child rather than immediately following the source node?

@monfera
Copy link

monfera commented Oct 26, 2017

just random 2¢ from the trenches, it'd be a good addition. I also used selection.create, selection.clone etc. (basically, the DOM API analogs) with my minimal cleanroom data binding experiment, made for learning purposes, that'd for example make it possible to build and equip a scenegraph before attaching to a node already under document, or would enable building vDOM fragments, or things for whatever serialization. Things looked quite natural, e.g.

fg.selectAll(...)
    .create('circle')
    .attr('cx', ...)
    .attr('cy', ...)
    .attr('r', ...)
    .append();

Then I thought there has to be a reason as to why the D3 API has most of these DOM operations coupled, likely, simplicity and resource management (DOM-bound gc is part of the hidden treasures of D3).

Here it also clones and inserts at the same time which fits in with current D3.

@monfera
Copy link

monfera commented Oct 26, 2017

...on a second thought for this use case (extra axis label), despite the general utility of clone and ilk, another option for the tick embellishment is to convert axes into two parts: layouts and shapes. Both are already natural part of the D3 structure. A normal use would just pipe the two, identical to current behavior; custom use would be pipe(makeLayout, amendViewModel, makeShapes) where the middle element would be free to copy eg. the last array element as data, modify its stuff (similar to how one can tamper with sankey dx etc.). Opening it up and making it pure data would perhaps be better than the above neat hack (for this purpose). Fits better with other paradigms too, eg. you could use a D3 axis layout with a react renderer, or you could build a reactive dataflow (observables, whatnot) if it's data-driven rather than DOM mutating.

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

2 participants