简体   繁体   中英

How to create an instance with the new constructor for every node in a json?

I am about to start writing an App which is an Organizational Chart and I will be using this library where the code example they provide is very clear:

        var options = new primitives.orgdiagram.Config();

        var items = [
            new primitives.orgdiagram.ItemConfig({
                id: 0,
                parent: null,
                title: "Scott Aasrud",
                description: "VP, Public Sector",
                image: "demo/images/photos/a.png"
            }),
            new primitives.orgdiagram.ItemConfig({
                id: 1,
                parent: 0,
                title: "Ted Lucas",
                description: "VP, Human Resources",
                image: "demo/images/photos/b.png"
            }),
            new primitives.orgdiagram.ItemConfig({
                id: 2,
                parent: 0,
                title: "Joao Stuger",
                description: "Business Solutions, US",
                image: "demo/images/photos/c.png"
            })
        ];

        options.items = items;
        options.cursorItem = 0;
        options.hasSelectorCheckbox = primitives.common.Enabled.True;

        jQuery("#basicdiagram").orgDiagram(options);

which is weird for me, is the items array, I will not be placing that array in the main file where I am going to be constructing my App. Actually the data will come from a json, so just imagine that items array coming from a separate json file, so . . . what should I do in order to put that data in a json, and then call it in my main file, and create the instances like this new primitives.orgdiagram.ItemConfig({ . . . }) for every node in that json?

What are your suggestions?

You can use [].map . It creates a new array with the values returned by a function called with each array item as an argument:

var items = JSON.parse(myjson).map(function(item) {
  return new primitives.orgdiagram.ItemConfig(item);
});

Actually, that definition of nodes via new object is done for the sake of code readability, in order to reference user to options of proper object type. Chart supports JSON objects as well.

        var options = new primitives.orgdiagram.Config();

        var items = [
            {
                id: 0,
                parent: null,
                title: "Scott Aasrud",
                description: "VP, Public Sector",
                image: "demo/images/photos/a.png"
            },
            {
                id: 1,
                parent: 0,
                title: "Ted Lucas",
                description: "VP, Human Resources",
                image: "demo/images/photos/b.png"
            },
            {
                id: 2,
                parent: 0,
                title: "Joao Stuger",
                description: "Business Solutions, US",
                image: "demo/images/photos/c.png"
            }
        ];

        options.items = items;
        options.cursorItem = 0;
        options.hasSelectorCheckbox = primitives.common.Enabled.True;

        jQuery("#basicdiagram").orgDiagram(options);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM