addNodeData displays only new data on the canvas

I want the addNodeData function to not add data that has been already added to digram.model. Basically what I aim to achieve is that duplicated data is not added to the diagram.model. Any suggestions?

GoJS uses mutable models and data. Please read GoJS Using Models -- Northwoods Software and the sections that follow it.

So each new data object in the Model.nodeDataArray will be represented by a Node.

If you don’t want to add a new node but want to modify an existing one, just modify its properties by calling Model.set.

If you don’t already have a reference to that existing object, call Model.findNodeDataForKey.

basically for my use case clicking on a specific node creates that node. Clicking on it a second time creates a second node which is exactly the same. I was hoping to find some logic through an if else condition. So that clicking the node for a seond time doesn’t initiate the addNodeData function. Could you help out with that?

Without knowing more about your use-case, it seems odd to allow the user to click the node multiple times if you only want one node to be created.

What code are you using in your click function to add the node data?


I mean the whole click function, including the definition of newItem.

You could do something like:


That didn’t work either properly either. It displays a single independent node instead of a child node(which is what it normally does and that’s how I prefer it to be). newitem is an object that’s added via myDiagram.model.addNodeData(newItem);

OK, then I do not understand what you want to do.
How many nodes do you want to add?
Have you prespecified what their keys should be?

there are parent nodes and each parent node should display a single child node on click. Yes I’ve done that

If you look at the model after you have made your changes/additions, is that data all correct? You could examine it by calling Model.toJson.

It’s correct yes. After clicking on a parent node it displays the child node. I want only one child node of that single parent and not multiple duplicate child nodes. Does this make sense?

Maybe your new node data can include a key. Then before calling addNodeData, you can check if that key already exists by calling Diagram.findNodeForKey.