Skip to content

Why have an insert method? #63

@samselikoff

Description

@samselikoff

Hello,

I'm trying to wrap my head around the insert method. In the world of vanilla D3, we typically append elements within the enter selection:

d3.select("body").selectAll("p")
    .data([4, 8, 15, 16, 23, 42])
  .enter().append("p")
    .text(function(d) { return "I’m number " + d + "!"; });

In d3.chart, we'd do something like this:

d3.chart('PChart', g, {
  ...
  insert: function() {
    return this.append('p');
  },

  events: {
    'enter': function() {
      this.text(function(d) { return "I’m number " + d + "!"; });
    }
  }
});

My question is, why not just append the elements in the enter lifecycle event? I may not be reading it right, but isn't this what the relevant code is essentially doing?

The only info I've been able to find about this was in Irene's blog post:

insert method - the method that creates the actual data-bound elements and sets any attributes that don’t have to do with the data. The reason we don’t do that here is because we want to account for our data potentially changing and thus want to delegate setting any data-driven attributes to the lifecycle events.

But isn't appending elements a data-driven operation, and therefore doesn't it belong in the enter lifecycle event?

I think I'm just missing something here - would love some clarification.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions