Using Binding without a data model

Do I have to have a Part bound to a data model (binding a model to the graph, associating a part through a LinkTemplateMap, etc.) to bind a value setting to a Part’s attribute?

Simplest use case: bind a Part’s visible attribute to a conversion function that pulls from a global variable only, and not caring about any model.

Why I’d want to do this: I’m creating a legend for the graph. I want it visible only if a global variable is true.

The problem: It doesn’t work if I use a conversion function that returns the global Boolean variable. If, however, I create the Part using the visible: false initializer then it is not shown, as expected. Even, however, simply returning false in the conversion function like the below doesn’t work.

this.diagram.add( $(go.Part, "Horizontal", { 'name': 'theLegend' }, new go.Binding('visible', '', () => false), $(go.Shape, "Circle", { width: 20, height: 20 }), $(go.TextBlock, "Hello World") ) );

I have also tried:

new go.Binding('visible', '', () => false).ofObject();

with same effect.

And that effect is that the part is shown on the map (it is visible) which is not what I expect.

Thanks,

Kris

That is correct – only modeled Parts have their Bindings evaluated.

You could make that Part be modeled by adding a new node template/category that is the legend that you want, and then adding a singleton instance of that category node data to the model by Model.addNodeData, rather than adding it via Diagram.add. Then its bindings will be evaluated just as for any other Node.

Note also the shared Model.modelData data object, which you can use for holding “global” variables for data binding. Call Binding.ofModel to change the Binding to use that single shared data object as the source.
https://gojs.net/latest/intro/dataBinding.html#BindingToSharedModelDataSource

That’s brilliant, thanks Walter! I opted for the first option (create singleton node, assigned using nodeTemplateMap). Works like a charm.

Thanks for you help,

K