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

New option for args to selection.append #724

Closed
wants to merge 1 commit into from

Conversation

theJohnnyBrown
Copy link

allows selection.append(function). The function is expected to return
a node for each member of the selection.

allows `selection.append(function)`. The function is expected to return
a node for each member of the selection.
@mbostock
Copy link
Member

Append returns the added nodes; your implementation returns the parent nodes. Also, you can implement this functionality already using select(function). For example:

selection.select(function(d, i) {
  return this.appendChild();
});

Or as part of the core library:

diff --git a/src/core/selection-append.js b/src/core/selection-append.js
index 1c57da1..4a1dc20 100644
--- a/src/core/selection-append.js
+++ b/src/core/selection-append.js
@@ -1,7 +1,5 @@
 // TODO append(node)?
-// TODO append(function)?
 d3_selectionPrototype.append = function(name) {
-  name = d3.ns.qualify(name);

   function append() {
     return this.appendChild(document.createElementNS(this.namespaceURI, name));
@@ -11,5 +9,11 @@ d3_selectionPrototype.append = function(name) {
     return this.appendChild(document.createElementNS(name.space, name.local));
   }

-  return this.select(name.local ? appendNS : append);
+  function appendFunction() {
+    return this.appendChild(name.apply(this, arguments));
+  }
+
+  return this.select(typeof name === "function" ? appendFunction
+      : ((name = d3.ns.qualify(name)).local ? appendNS
+      : append));
 };

You haven't said why you want this functionality, so I'd err on the side of leaving it out. If you want to argue for this being useful, feel free to reopen.

Related #4 #311

@mbostock mbostock closed this Jul 24, 2012
mbostock added a commit that referenced this pull request Jun 30, 2013
Like selection.select, selection.append and selection.insert can now accept a
function which returns a node. This makes it slightly easier to append or insert
elements whose name is computed from data, or to append elements that already
exist (say from an element pool).

There has been much discussion regarding whether the function should return the
name of the element or the element itself. Returning a name is less work for the
caller, but only supports creating new elements; returning a name is also more
consistent with how D3 defines attribute values, but D3 does not allow attribute
names to be specified as functions. So, it seemed better to opt for consistency
with selection.select and selection.selectAll, which accept functions that
return elements, since this is more expressive. Of course, you can still use
select and selectAll to append elements, but using append to do that directly is
more intuitive.

Related #4 #311 #724 #732 #734 #961 #1031 #1271.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants