首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在GoJS中diagram.model.nodeDataArray的长度与diagram.findNodesByExample({})的结果不同

为什么在GoJS中diagram.model.nodeDataArray的长度与diagram.findNodesByExample({})的结果不同
EN

Stack Overflow用户
提问于 2017-10-16 08:43:07
回答 2查看 541关注 0票数 0

在图的第一个加载中,我在模型中添加了三个元素:

代码语言:javascript
复制
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 

然后使用goType删除两个项:使用diagram.model.removeNodeData方法的'tcp',并将它们再次添加到模型中:

代码语言:javascript
复制
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
}]);

但是在此之后,图中节点的数量就不同了,我在画布上只看到两个节点:

代码语言:javascript
复制
console.log(diagram.model.nodeDataArray.length); //3
console.log(diagram.findNodesByExample({}).count); //2

如果查看使用每个方法的diagram.findNodesByExample({})的结果,我会发现只添加了instance2

代码语言:javascript
复制
diagram.findNodesByExample({}).each(function (item) {
   console.log(item.data.goKey);
});
// instance1
// instance2

我做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-08 14:31:18

问题终于找到了。从模型中删除节点(我将它们保存在副本中)之后,再次添加它们,因此,如果查看这些节点,我们将看到一个额外的属性__gohashid,在再次将其添加到模型之前,应该删除该属性。我不知道它是怎么工作的,但是这串代码

代码语言:javascript
复制
delete node.__gohashid;

修复上面描述的问题。希望它能对某人有用。

票数 0
EN

Stack Overflow用户

发布于 2017-11-02 13:37:22

我刚试过你的代码,但没能重现这个问题。以下是我使用的整个页面:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<title>Minimal GoJS Sample</title>
<!-- Copyright 1998-2017 by Northwoods Software Corporation. -->
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/1.7.28/go-debug.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<script id="code">
  function init() {
    var $ = go.GraphObject.make;

    diagram =
      $(go.Diagram, "myDiagramDiv",
        {
          initialContentAlignment: go.Spot.Center,
          layout: $(go.GridLayout)
        });

    diagram.nodeTemplate =
      $(go.Node, "Vertical",
        $(go.TextBlock, new go.Binding("text", "goKey")),
        $(go.TextBlock, new go.Binding("text", "goType"))
      );

    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 
  }

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

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

    console.log(diagram.model.nodeDataArray.length); //3
    console.log(diagram.findNodesByExample({}).count); //2??? -- No, I get 3
    diagram.findNodesByExample({}).each(function(item) {
      console.log(item.data.goKey);
    });
  }
</script>
</head>
<body onload="init()">
  <div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
  <button onclick="replaceTwo()">Replace Two</button>
</body>
</html>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46766201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档