Select Part after model commit/update

Hi Team,

I’m working on a Vue.js application which has a watcher on internal repositories of data and if anything has changed, fire updateModel function.
The updateModel does model.commit with changing nodeDataArray on a model. After this commit the part previously selected becomes unselected (which is probably right).

What I’m trying to do is to bring back the selection so I save diagram.selection before the operation and do diagram.selectCollection(previousCollection) afterwards.

Even though the debugger shows the previous selection correctly (the same Parts, the same __gohashid) selecting collection doesn’t seem to do a job (I have ChangedSelection listener to do other things but what comes in the event.diagram is selection not containing anything really).

I would appreciate pointing me in the direction of what I may be doing wrong. I can provide the code snippets and my general set up if needed.

Thanks!
Michał

Are you replacing the Model.nodeDataArray? If so, that will remove all Nodes and create new Nodes, which is why the Diagram.selection has to be cleared of all Nodes.

Call Model.mergeNodeDataArray instead. Model | GoJS API

If GraphLinksModel, also GraphLinksModel | GoJS API – but note that requires unique keys on link data.

I am replacing nodeDataArray in fact, yes.

Thanks for pointing out merge method. I will try that and see if it helps.

Even if I replace the nodeDataArray but still have any data which would identify the part after the operation, I guess there is a way to find it after the operation?

All references (i.e. pointers) to Parts remain valid after modifying data properties, whether by set… or *merge…**, including changing category/template.

If your data has the same key, call either Model.findNodeDataForKey (which is what merge… calls) or Diagram.findNodeForKey.

1 Like

Thanks a lot!

I assume that merge will react properly also if I remove elements from the model?

Yes, if the given Array doesn’t have a data object with an existing key, the existing Node will be removed.

Thanks a lot!