2017-10-16 51 views
0

在圖的第一負載我添加在我的模型三個要素:爲什麼在GoJS diagram.model.nodeDataArray的長度從diagram.findNodesByExample的結果不同({})

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:「TCP」使用diagram.model.removeNodeData方法和模型中再次添加它們:

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 
}]); 

但畢竟這在圖不同節點的數量,我只看到兩個在畫布上的節點:

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

如果看一看diagram.findNodesByExample({}),使用方法每個,我看到INSTANCE2才被加入的結果:

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

我做錯了什麼?

回答

0

問題終於找到了。從模型中刪除節點(我將它們保存在副本中)後,我再次添加它們,因此,如果查看這些節點,我們會看到一個額外的屬性__gohashid,在再次將其添加到模型之前應該將其刪除。我不知道它是如何工作的,但這串代碼

delete node.__gohashid; 

修復了上述問題。希望它對某人有用。

1

我只是試過你的代碼,但無法重現問題。以下是我使用的整個頁面:

<!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> 
+0

謝謝你的回答,但問題是重複使用已刪除的節點。刪除** __ gohashid **後,它已被修復。 – blackhard

相關問題