Update data property in InitialLayoutCompleted


#1

Hi,

I am updating a data property in the InitialLayoutCompleted DiagramEvent:

diagram.startTransaction("Update nodes' valid property");
diagram.model.setDataProperty(node.data, "_valid", isValid);
diagram.commitTransaction("Update nodes' valid property");

This works when I initially create the diagram and set the model. But when I clear the diagram and set a different model, the binding for the “_valid” property ( borderShape.bind(new go.Binding("visible", "_valid");) does not get evaluated.
However, wrapping the data property update in a setTimeout call works:

setTimeout(() => {
 diagram.startTransaction("Update nodes' valid property");
    diagram.model.setDataProperty(node.data, "_valid", isValid);
    diagram.commitTransaction("Update nodes' valid property");
}, 100);

Is it a bad idea to update a data property in the InitialLayoutCompleted event listener? Or is there most likely some other issue in my code?


#2

One comment: the “InitialLayoutCompleted” DiagramEvent runs within the transaction, so you don’t need to start and commit the transaction. But I suppose there isn’t too much harm from having a nested transaction. On the other hand, if you are going to execute it “later”, then clearly you’ll need to perform a transaction, so the modification upon setTimeout does need the transaction.

Whether it’s a bad idea to modify model data in an “InitialLayoutCompleted” DiagramEvent listener depends on what effect that data change has. If it changes some node visibility, that might invalidate the layout again. In general I guess it’s a bad idea, but there are certainly situations where it will work reliably.


#3

Thanks for your reply!
The data change modifies the visibility of some nodes’ borders.
However, I can confirm that modifying the data property in the InitialLayoutCompleted works just fine. The issue appearently was that for some other functionality I modified the diagram/model directly after replacing the model (in the next line basically), instead of using the InitialLayoutCompleted event for that. I guess that messed something up. After fixing this, it works just fine without the timeout.