convert js graphs to backbone graphs for composite views

by bordev

JavaScript

var dto = {
            "id": "0001",
            "type": "donut",
            "name": "Cake",
            "ppu": 0.55,
            "batters":
                {
                    "batter":
                        [
                            { "id": "1001", "type": "Regular" },
                            { "id": "1002", "type": "Chocolate" },
                            { "id": "1003", "type": "Blueberry" },
                            { "id": "1004", "type": "Devil's Food" }
                        ]
                },
            "topping":
                [
                    { "id": "5001", "type": "None" },
                    { "id": "5002", "type": "Glazed" },
                    { "id": "5005", "type": "Sugar" },
                    { "id": "5007", "type": "Powdered Sugar" },
                    { "id": "5006", "type": "Chocolate with Sprinkles" },
                    { "id": "5003", "type": "Chocolate" },
                    { "id": "5004", "type": "Maple" }
                ]
        };

        function recurse(source, target) {

            var typestring, att, model, value;

            if (!target) {
                target = new Backbone.Model();
            }

            for (var key in source) {
                att = source[key];
                typestring = Object.prototype.toString.call(att);
                // handle object arrays
                if (typestring === "[object Array]") {
                    value = new Backbone.Collection();
                    for (var i = 0; i < att.length; i++) {
                        model = new Backbone.Model();
                        value.add(model);
                        recurse(att[i], model); // recursion
                    }
                    // handle object types
                } else if (typestring === "[object Object]") {
                    value = new Backbone.Model();
                    recurse(att, value); // recursion
                    // handle value types
                }...