Should TreeModel validate against cycles automatically

I am using a TreeModel for a tree structured diagram. My understanding that this model has a built in validation to prevent users to draw a link back to the parent as well as prevent any cycles in the tree. If that’s the case, please confirm. I am still able to draw a link back to the parent and in some cases I see other existing links in the diagram disappear when doing that. I use GoJS version 2.

Yes, that’s true.

So, for example, this is what happens when I draw a link from Beta to Gamma. The link from Alpha to Gamma disappears at that point and it allows a cycle to be created.
Screenshot 2024-07-03 at 3.03.08 PM
Screenshot 2024-07-03 at 3.02.57 PM

I found a solution to this problem by providing a validation function to the node template properties which prevents the link to be connected to a node with no parent like so:

linkValidation: (_fromNode: Node, _fromPort: any, toNode: Node) => {
    return !!!;

That’s an interesting bug. Thanks for reporting it.

It’s actually easier if you just set Diagram.validCycle:

  new go.Diagram("myDiagramDiv", {
      validCycle: go.Diagram.CycleDestinationTree,
      . . .

That will work for both GraphLinksModels and TreeModels.

1 Like

This will be fixed for 3.0.8, although you don’t need it since you can easily set Diagram.validCycle or your custom linkValidation predicate.

@walter Indeed your suggestion looks even better. Thank you! It would be great if this could also be fixed for those who use version 2. For me it’s okay, but others may not be aware.