this code, after click button reload Json it will emit errors “Change not within a transaction: !d position: -1 old: Point(54,54) new: Point(66,67)…” and don’t fire any changed event.
var diajson = `
{
"nodeDataArray": [
{
"loc": "50 50",
"text": "new node"
}
],"linkDataArray": [],"linkKeyProperty": "id","class": "go.GraphLinksModel"
}
`;
var $go = go.GraphObject.make; // for conciseness in defining templates
var diagramA =
$go(go.Diagram, "DDiv", // must name or refer to the DIV HTML element
{
hasHorizontalScrollbar: false, "animationManager.isEnabled": false,
hasVerticalScrollbar: false,
initialAutoScale: go.Diagram.Uniform,
"toolManager.mouseWheelBehavior": go.ToolManager.WheelZoom,
"undoManager.isEnabled": true, // enable Ctrl-Z to undo and Ctrl-Y to redo
});
// define the Node template
diagramA.nodeTemplate =
$go(go.Node, "Vertical", { resizable: true },
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
$go(go.Panel, "Spot",
$go(go.Shape, "Rectangle", {
width: 80, height: 80, margin: 4, fill: "#99bce8", cursor: "pointer"
})),
$go(go.TextBlock, {
margin: 5, editable: true, textAlign: "center",
font: '13px Verdana, Arial, sans-serif,"ヒラギノ角ゴ Pro W3","メイリオ","Meiryo","Helvetica Neue","Helvetica"'
},
new go.Binding("text", "text").makeTwoWay()
)
);
diagramA.addDiagramListener("Modified", digmModified);
function digmModified(e) {
console.log( ">>Modified Diagram<<: "+ e.diagram.diagramKey + " " + e.diagram.isModified);
}
diagramA.addModelChangedListener(function(evt) {
// ignore unimportant Transaction events
if (!evt.isTransactionFinished) return;
var json = evt.model.toIncrementalJson(evt);
if (evt.oldValue == "Initial Layout") return;
console.log(json);
console.log(">>>>>>>>>>>>>>>>>>>>>" + evt.oldValue);
var txn = evt.object; // a Transaction
if (txn === null) return;
});
diagramA.model = go.Model.fromJson(diajson);
$("#loadbtn").click(function(){
diagramA.model = go.Model.fromJson(diajson);
diagramA.delayInitialization(
function () {
diagramA.zoomToFit();
}
);
});
why I use delayInitialization to call diagram.zoomToFit(); is, sometimes some diagram not fit zoom at the first time load json.