Quantcast
Channel: GoJS - Northwoods Software
Viewing all articles
Browse latest Browse all 7069

Adding new nodes after removing others doesn't work

$
0
0

@Giosk wrote:

I'm using react and gojs and I implemented a GraphLinksModel that fits perfectly my needs.
I'm using the contextMenu and after clicking on a button, if this is the root, I clear all the children then I add new ones.
This is the relative code:

//function for clearing nodes and links
removeNodesAndLinks = nodeId => {
    const nodes = myDiagram.model.nodeDataArray
    if (nodeId === 1 && nodes.length > 1) {
      nodes.filter(node => node.NodeId !== 1).forEach(n => {
        // remove links by selection
        const node = myDiagram.findNodeForKey(n.key)
        myDiagram.selectCollection(node.findLinksConnected())
        myDiagram.commandHandler.deleteSelection()
        //remove nodes
        myDiagram.model.removeNodeData(n)
      })
    }
  }

//this is the addChild function
const addChild = newnode => {
  const selnode = myDiagram.selection.first()
  if (!(selnode instanceof go.Node)) return
  myDiagram.startTransaction("add node and link")
  myDiagram.model.addNodeData(newnode)
  const newlink = { from: selnode.data.key, to: newnode.NodeId }
  myDiagram.model.addLinkData(newlink)
  myDiagram.commitTransaction("add node and link")
}

//this is the add new nodes function
const addNodes = data => {
  data.forEach(node => {
    addChild(node)
  })
}

PROBLEM
If I run addNodes or removeNodesAndLinks one by one, they work perfectly, but if I run the remove and subsequently the add Nodes as I do in the button action it works only the removing phase and not the adding phase.

This is the code in which I create the button and add the action:

this.makeButton(
      "NameA",
      (e, obj) => {
        const contextmenu = obj.part
        const nodedata = contextmenu.data
        this.removeNodesAndLinks(nodedata.NodeId)
        this.fetchTheData("NameA") //after the fetch it run addNodes
      },
     // visiblePredicate
    )

Maybe the problem is that this piece of code, must implement the fetch data function as a callback or as an async/await function?

const node = myDiagram.findNodeForKey(n.key)
myDiagram.selectCollection(node.findLinksConnected())
myDiagram.commandHandler.deleteSelection()
myDiagram.model.removeNodeData(n)

Thanks

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 7069

Trending Articles