State for the entire diagram

Currently we’re using model’s node array and link array for all the data and bindings but there are some states that are not specific to a single node. For instance, I want to have a mode which could get design | view value depending on some toggle buttons on the page.

Is there anyway we can store this state on then diagram and in the bindings use it. Something like these imaginary snippets:

// Usage
.bind(new go.Binding('fill', 'blah', blah => { 
  return diagram.state.mode === 'design' ? blah : "default";
}))

// Update
diagram.commitState('mode', 'view');

Yes, you can use modelData:

1 Like

So, for example:

new go.Shape(...)
  .bindModel("fill", "mode", mode => mode === "view" ? "white" : "green")

and to modify the state:

myDiagram.model.commit(m => m.set(m.modelData, "mode", "view"))
1 Like

Dang, how have I not seen this before. Thank you both.

Such bindings are distinctly slower than regular bindings, since any change to Model.modelState might need to change everything in the Diagram. So don’t use them unnecessarily.