Okky
Okky

Reputation: 10466

Knockout map nested JSON array

I was trying to make a knockout mapped JSON schema editor in table format. I successfully mapped json object, but issue is when i'm dealing with json array.

Code

var l_Model;
//Create model
l_Model = function(l_data) {
    var self = this;
    //map data from json as observables
    ko.mapping.fromJS(l_data, {}, self);
    //observable to get equation from json
    self.Eqn = ko.observable(null);
};
//view model mapping to html 
var viewModelMapping = {
    'dataModel': {
        create: function(options) {
            return new l_Model(options.data);
        }
    }
};
var vm = ko.mapping.fromJS(Data);

This code works as long as data is

data = {}

But When I deal with array of json objects I'm not able to map it

data =[{},{},{},.....]

I'm getting error in the console

Uncaught ReferenceError: Unable to parse bindings.

JS Fiddle

Upvotes: 1

Views: 710

Answers (1)

Pablo Rodríguez
Pablo Rodríguez

Reputation: 436

You can map your object from the root of the array and process each item and separated objects. You can even have another mapper inside your object and continue processing data with nested mappers

       //mapper method
       mapCollection = {
            '': {
                create: function (options) {
                    return new objectWithAnotherMapper(options.data);
                }
            }
        },

Upvotes: 1

Related Questions