Changing color of link depending on node type

I am using a LinkTemplate to display my diagram. I want to change the link color depending on a parameter bound from the model as follows

 var myDiagram = $(go.Diagram, containerId, {
     initialAutoScale: go.Diagram.UniformToFill,
     initialContentAlignment: go.Spot.TopLeft,
     layout: $(go.TreeLayout)			
 });

myDiagram.linkTemplate = $(go.Link, {
}, $(go.Shape, {strokeWidth: 2}, new go.Binding("stroke", "defaultColor", function(a){
    a == true ? return "#00B5CB": return "#F47321";
})) 
);

And the model is as follows

var nodeDataArray = [
    { key: "Alpha", defaultColor: true}, 
    { key: "Beta", defaultColor: false} 
];

myDiagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

It’s not working and i don’t know why.Thanks for the help!

That Binding in your link template:

is saying that the value of Shape.stroke should get the value of Link.data.defaultColor, after applying a conversion function.

But there’s no “defaultColor” property on your link data. (Actually, you don’t show it, but that’s what I assume is the case). So the Binding is not evaluated, leaving the Shape.stroke as the default color, black.

Perhaps you could use a Binding like that used in the multi-color validation example on http://gojs.net/latest/intro/validation.html. (Scroll most of the way down – it’s just before Grouping validation.)

new go.Binding("stroke", "fromNode", function(n) { return n.data.color; }).ofObject()

This should work well, even if the user reconnects the link using the RelinkingTool.

But note that if you call Model.setDataProperty on your node data, it won’t automatically update all of the bindings on the links that connect with the corresponding node. The use of the conversion function “hides” that dependency from the binding mechanisms.

Thanks so much. Perfect answer!