Search code examples
knockout.jsknockout-2.0knockout-mapping-plugin

getting multidimensional array (object) observable in KnockoutJS


I am building an application with Knockout and find it very useful. Although, I have a problem with getting multidimensional array (object) observable.

At the moment I am using following structure:

    self.form = ko.observableArray(ko.utils.arrayMap(initialData, function(section) {
        var result = { 
            name : section.name, 
            code : section.code, 
            type : section.type, 
            fields: ko.observableArray(section.fields) 
        };
        return result;
    }));

It works well, but I can't get it working if the initialData is more than two levels. I tried something like

    self.form = ko.observableArray(ko.utils.arrayMap(initialData, function(block) {
        var result = { 
            name : block.name, 
            code : block.code, 
            type : block.type, 
            sections: ko.observableArray(ko.utils.arrayMap(block.sections, function(section) {
                var result = { 
                    name : section.name, 
                    code : section.code, 
                    type : section.type, 
                    fields: ko.observableArray(section.fields) 
                };
                return result;
            }))
        };
        return result;
    }));

The final array structure looks good, but knockout doesn't updates DOM when I am doing push to sections array:

    self.addField = function( section ) {
        field = {
            code: uid(),
            name: "New Field",
            value: '',
            type: section.type
        };
        section.fields.push(field); 
    };

I also tried a knockout.mapping.js plugin (is that a right approach?) looks good first, but after a push in the function above I have my new field element not observable, just object.

The plugin doumentation says:

// Every time data is received from the server:
ko.mapping.fromJS(data, viewModel);

But I am not sure that it is my case.

If anyone has any ideas, it would be much appreciated.

Thanks.

UPD: It is not a problem to make 1st and 2nd levels observable, problem is to go deeper.

Here is an example of initialData:

var blocks = [
    {
        "name" : "",
        "sections" : [
            {
                "name" : "Section 1",
                "fields" : [
                    {
                        "name" : "Field A",
                        "type" : "checkbox",
                        "code" : uid()
                    }
                ]
            }
        ]
    }
];

HTML

<div data-bind='template: { name: tpl-form-field-checkbox, foreach: fields }'></div>
<button class="btn addField" data-bind="click: $root.addField">Add</button>

<script type="text/html" id="tpl-form-field-checkbox">
    <input type="text" name="" value="<%= name %>" /> <br/>
</script> 

Solution

  • The mapping plugin is the best way to go. It will automatically map your objects into observables and observableArrays, so you don't have to do it manually.

    Here is a simple fiddle that may give you some pointers: http://jsfiddle.net/jearles/CGh9b/

    In this example I create a tree structure and them allow you to add a new entry. You can see that I am able to continue to add at increasingly deeper levels with no problem, and because the names are observable they can be changed.