Search code examples
javascriptgojs

Why in GoJS the length of diagram.model.nodeDataArray differs from the result of diagram.findNodesByExample({})


On the first load of diagram I add three elements in my model:

var model = new go.GraphLinksModel();
model.nodeKeyProperty = 'goKey';
model.nodeCategoryProperty = 'goType';
model.addNodeDataCollection([
  {
      goKey: 'instance1',
      goType: 'component',
      //other data
  }, {
      goKey: 'instance2',
      goType: 'tcp',
      //other data
  }, {
      goKey: 'instance3',
      goType: 'tcp',
      //other data
  }]);
diagram.model = model;
console.log(diagram.findNodesByExample({}).count); //3
console.log(diagram.model.nodeDataArray.length); //3 

Then I remove two items with goType: 'tcp' using diagram.model.removeNodeData method and add them again in the model:

var item2 = _.find(diagram.model.nodeDataArray, {goKey: 'instance2'});
var item3 = _.find(diagram.model.nodeDataArray, {goKey: 'instance3'});
model.removeNodeData(item2);
model.removeNodeData(item3);
console.log(diagram.model.nodeDataArray.length); //1
console.log(diagram.findNodesByExample({}).count); //1

diagram.model.addNodeDataCollection([{
     goKey: 'instance2',
     goType: 'tcp',
     //other data
  }, {
     goKey: 'instance3',
     goType: 'tcp',
     //other data
}]);

But after this the amount of nodes in diagram differs and I see only two nodes on canvas:

console.log(diagram.model.nodeDataArray.length); //3
console.log(diagram.findNodesByExample({}).count); //2

If take a look at the result of diagram.findNodesByExample({}) using method each, I see that the instance2 was added only:

diagram.findNodesByExample({}).each(function (item) {
   console.log(item.data.goKey);
});
// instance1
// instance2

What am I doing wrong?


Solution

  • The problem was finally found. After removing the nodes from model (I save them in copies), I added them again, so if take a look at these nodes, we will see an extra property __gohashid that should be removed before adding it to the model again. I don't know how it works, but this string of code

    delete node.__gohashid;
    

    fixes the problem described above. Hope it will be useful for somebody.