Clean up diagram before creating new model

In our app if the user clicks Refresh we pull new data from server and want to create new nodes, links, and a new model from it since it could have changed. Here is my code that does that. Before calling this code diagram.model is already pointing to existing model.

    me.nodes = me.buildNodes();
    me.links = me.buildLinks();
    me.diagram.model = new go.GraphLinksModel(me.nodes, me.links);

Sometimes I’ll get the following error in the console and I don’t think I’m manually starting any transitions.

Uncaught Error: Do not replace a Diagram.model while a transaction is in progress.


This doesn’t happen all the time and I’m having a tough time tracking down. I’m wondering if I should do some clean up first (possibly in transaction) before setting diagram.model to a new model instance? If so what all should I do?

Thanks

Found diagram.clear() which seemed to do the trick. Let me know if there is anything else I should do.

Are you setting the Diagram.model in code that is invoked asynchronously when the data has arrived successfully? It might be that you are doing so during the middle of a transaction, such as during a drag operation by the user.

To avoid that problem, execute:
me.diagram.currentTool.doCancel();
before you execute:
me.diagram.model = …

That will cancel any tool that is ongoing, so there shouldn’t be any transaction happening. If you want, you can check for that by looking at Diagram.undoManager.isInTransaction.

Yes the data is loaded asynchronously from an Ajax call. I think the scenario is if a node is selected, click the refresh button, then rebuild the diagram, there is an internal transaction going on with no name. The diagram.clear() fixed that but I’ll also add doCancel() after the clear().

17 posts were split to a new topic: Change in 2.0.0 when assigning keys to nodes dropped from palette