Access property of parent group from template

Hello,

Is it possible to access a property of the parent group from within a template that is used for subgroups ?
I though about using containingGroup but I didn’t find how.

Here an example of what I’d like to achieve. Subgroup text would be of the color value from the parent group, MainGroup.

var nodeDataArray = [
    // Groups
    { key: "MainGroup", isGroup: true, color: "red"},
    { key: "Subgroup", isGroup: true, group: "MainGroup", category: "subgroup" },
];

var subgroupTemplate =
    $(go.Group, "Vertical",
        $(go.TextBlock,
            { alignment: go.Spot.Left },
            new go.Binding("text", "key"),
            new go.Binding("stroke", >>>containingGroup.color<<<))
);

diagram.groupTemplateMap.add("subgroup", subgroupTemplate);

Best regards,

Michaël Polla

In general Bindings do not work between Parts – just within a Part and its data.

You could try something like:

new go.Binding("stroke", "containingGroup", function(g) {
  return g ? g.data.color : "black";
}).ofObject()

But such bindings are unreliable, because if the “MainGroup”'s data.color is changed, even by calling Model.set, other Parts such as the “Subgroup” won’t know about it.

1 Like

That was fast…! Thank you @walter for this nice solution ! It worked as expected.

I’ll keep in mind your remark though, and will ensure that I don’t change the binded property value later on.

Thanks again ! Have a nice day. :-)