Search code examples
jsongojs

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


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?


Solution

  • 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 https://gojs.net/latest/api/symbols/Model.html.

    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.

    This is a duplicate of https://forum.nwoods.com/t/how-to-add-new-property-to-node-template-that-will-be-saved-to-json-diagram-model/8726.