nodeTemplateMap inheritance?

Hi, i’m trying to create many nodeTemplateMap with different categories, and i’m finding myself writing repeating a lot of the code. So is there a way I can define a base nodeTemplate then for each different nodeTemplateMap I’ll just add/overwrite some code.

Something like the picture I’ve attached?

You can certainly GraphObject.copy() whatever you want, and then modify the resulting GraphObject. Just be sure to do so before you assign the template or template Map to a Diagram.

For example, take a look at what the Data Flow sample does to create templates: http://gojs.net/latest/samples/dataflow.html

copy() . Doesn’t seem to property clone the bindings though.

var goStandard = … // CODE TO build go.Node template with related go.Bindings
var goPalette=goStandard.copy(); // deep recursive copy of template, assumably

// two diagrams assigning different template
myDiagram.nodeTemplateMap.add("", goStandard)
myPalette.nodeTemplateMap.add("", goPalette); // Bindings don’t apply here!

You’re right. We’ll investigate this. However, this might just be a restriction on the functionality for the time being.

As the DataFlow sample demonstrates, you can certainly create multiple similar templates programmatically without using GraphObject.copy.

First, I’d like to remind everyone that if you want to share unmodified templates, you can. In this situation I assume you want to modify copied templates and use them as templates.

OK, for now here’s how you can copy and then modify a template:

    // create the node template for the main Diagram
    var diagramNodeTemplate =
      $(go.Node, "Auto", nodeStyle(),
        $(go.Shape, borderStyle()),
        $(go.TextBlock, textStyle(true),
          new go.Binding("text").makeTwoWay())
      );

    // copy the node template for the Palette
    diagramNodeTemplate.data = {};
    diagramNodeTemplate.data = null;
    var paletteNodeTemplate = diagramNodeTemplate.copy();

    // and modify the palette's node template
    paletteNodeTemplate.elt(0).fill = "lightgreen";
    paletteNodeTemplate.elt(1).font = "bold 12pt sans-serif";

Of course the details of the main diagram’s template(s) do not really matter. But remember that you cannot add, remove, or modify any Bindings once the template has been copied.

Perhaps in some future version we will have a Panel.copyTemplate() method or we can figure out a way for copy() to figure out if the intent is to copy a template or if the intent is to create an instance of the template.

1 Like