I have been struggling for a while now trying to sort this out. I have the following scenario:
If I add new nodes to the model data using diagram.model.addNodeData, my lines work perfectly.
However, I have a problem where I need to add the data to the model via a different route. If I do not disable “addNOdeData”, I end up with duplicates. So here is what I do:
Create a new JavaScript object and add it to my back-end data model.
Create a new go.Node object.
Set the node’s data property to the newly created object
Add the node to the diagram using diagram.add
If I use this approach and I try to create a new link from other nodes to this new node, the link disappears, even though it is added to the diagram’s data.linkDataArray object. I am desperate to get this sorted out.
If anybody has any ideas or need more details, please let me know…
My guess is that the link that you defined wasn’t able to resolve the references to the nodes it wanted to connect to.
Creating a separate Node for each node data in your model is what the Diagram does automatically. I think we should concentrate on figuring out why you are getting duplicate nodes when calling Model.addNodeData. As you can see in the State Chart sample, and probably in other samples too, adding a node and a link to that new node is fairly straight-forward. See the addNodeAndLink function in State Chart.
So in my scenario I have a couple of different places from where the underlying object model can be updated. One of them is the actual GoJS canvas. Another place is somewhere completely out of the canvas’ scope. Eg. consider a button on the HTML page that, when clicked, adds a new item to the underlying array of nodes. In this case, the button doesn’t have access to the GoJS canvas, and thus, cannot invoke the “addNodeData” method. This is the reason I have to be able to trap change events on the object model (this already works), and then create the GoJS Node manually. If I don’t create it manually, this scenario is what causes the duplicate item on the model.
“addNodeData” is a Model method, not a Diagram method.
So are you saying that your app has a model that is mirrored by the GoJS Model data (in nodeDataArray and linkDataArray)? That’s OK – your model’s change events should update the GoJS Model appropriately using the GoJS model’s methods.