Model data should not have any references to a Diagram or any part of a diagram

I am currently writing some unittest via using gojs models.

I have following code in typescript.

let model = new go.GraphLinksModel();
let node = new go.Node();
let nodeData = model.copyNodeData(node); // warning here

model.startTransaction(“Add node”);
model.addNodeData(nodeData);
model.commitTransaction(“Add node”);

There is no problem if I get the model from a real diagram by using the way any sample provided. However, I get following log warning now.

LOG: ‘Warning: found GraphObject or Diagram reference when copying model data on property “port” of data object: Node#1114
Model data should not have any references to a Diagram or any part of a diagram, such as: Node#1114’
LOG: ‘Warning: found GraphObject or Diagram reference when copying model data on property “bc” of data object: Node#1114
Model data should not have any references to a Diagram or any part of a diagram, such as: Node#1114’
LOG: ‘Warning: found GraphObject or Diagram reference when copying model data on property “locationObject” of data object: Node#1114
Model data should not have any references to a Diagram or any part of a diagram, such as: Node#1114’
LOG: ‘Warning: found GraphObject or Diagram reference when copying model data on property “self” of data object: Node#1114
Model data should not have any references to a Diagram or any part of a diagram, such as: Node#1114’

Any idea? This won’t break the test but just a little bit annoying.

Thanks,
Chuan

No, that warning is important for you to recognize that something is very wrong.

In this case it should be clear that you are trying to add a Node to the model, rather than some node data, a JavaScript Object.

Each Node should belong to a Diagram.

Each Node.data object should belong to a Model. (Actually it will be in its Model.nodeDataArray.)

The whole point of models is that in a model-view architecture there should be no references to a view within the model. There are many advantages of maintaining such a separation, including the ability to easily serialize the model.

Thanks for quick reply, Walter.

I am still a little bit confused.

The parameter of copyNodeData is required to be a Node. The output is the copied data with it and will be add to model later. So are you saying that I cannot create a node by using node constructor but must bind a node to a diagram?

Can you help me modify the usage of my code? I don’t want to create a view since I only want to test the model part. How should I do the correct thing in order to add a nodedata correctly to a model without any view/diagram?

-Chuan

No, the documentation at Model | GoJS API is clearly talking about data, not about Nodes. That’s why “Data” is part of the method name. Also see Page Not Found -- Northwoods Software

copyNodeData(nodedata: Object): Object;

Yes, you can do a lot of stuff with Models without creating any Diagrams. But maybe less than you’d like.

Wait, am I misunderstanding this? Seems true.

Parameters:
{Object} nodedata
a JavaScript object represented by a node, group, or non-link.

-Chuan

I change my code to following now. But get different error now. I am using Karma + PhantomJS + Jasmine for my test.

TypeError: Attempted to assign to readonly property.

let model = new go.GraphLinksModel();
let node = new go.Node();
let nodeData = model.copyNodeData(node.data); // no warning now
nodeData[“test”] = “something”; // error here

model.startTransaction(“Add node”);
model.addNodeData(nodeData);
model.commitTransaction(“Add node”);

Yes, and what’s the value of nodeData?

That’s because node.data is null.

The word “represent” derives from the word “present”. If “X is represented by Y”, then Y presents a symbol or rendering of X. In GoJS (or any model-view system) X is some piece of data or computer-memory and Y is what you see on screen or paper.
Ceci n’est pas une pipe

You are right, my nodeData is not defined. Actually I don’t need to create a dummy node at all. Can just model.copyNodeData({})