How to add new property to node template that will be saved to Json diagram model?

I mean on type of application with palette on the left and diagram area on the right side.

For example I have the following node template:

var inputTemplate =
    $(go.Node, "Spot",
    nodeStyle(),
    $(go.Shape, "Circle", shapeStyle1(),
        { fill: red }),  // override the default fill (from shapeStyle1()) to be red
    $(go.Shape, "Rectangle", portStyle(false),  // the only port
        { portId: "", alignment: new go.Spot(1, 0.5) }),
    {
        doubleClick: function (e, obj) {
            window.open("http://www.microsoft.com", "_blank");
    }}
    );

And want to add Guid property on node level that will appear in Json model when we save the diagram using toJson function. How to do it?

You can add as many properties as you like to the model data. Just be sure that the property name does not start with an underscore (“_”) and that the property value is JSON-serializable. Read about the requirements at Model | GoJS API.

If you want to use GUIDs as key values, first make sure that all of your existing node data have unique GUID string values. And set Model.makeUniqueKeyFunction to a function that returns a new GUID string.

I wrote the following node template:

var inputTemplate =
    $(go.Node, "Spot",
    new go.Binding("guid", "num"),
    nodeStyle(),
    $(go.Shape, "Circle", shapeStyle1(),
        { fill: red }),  // override the default fill (from shapeStyle1()) to be red
    $(go.Shape, "Rectangle", portStyle(false),  // the only port
        { portId: "", alignment: new go.Spot(1, 0.5) }),
    {
        doubleClick: function (e, obj) {
            window.open("http://www.microsoft.com", "_blank");
    }}
    );

You can see there is new “guid” property, but I’m not sure if I did it correctly. In any case, there is no “guid” in saved Json model. Another thing, where can I set the values for “guids”.

Regarding guids, we’d like to have unique identifier for each node and that’s why we need guids. Is there something else instead of guids that diagram’s node already have?

As I said, and as GoJS Using Models -- Northwoods Software and GoJS Data Binding -- Northwoods Software discuss, you should put all of your application-specific properties on the model node data (and model link data), not on the Node (and Link) elements in the Diagram.

There is no “guid” property on Node, so you might get a Binding warning about that being an undefined property.

And only model data will be saved by Model.toJson or recovered by Model.fromJson.