1

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

5
  • Please edit your post with the relevant parts of your code, because if JSFiddle is down nobody can understand your question! Commented Aug 7, 2013 at 5:30
  • 3
    Your question looks better now, but it still doestn't contain enough details for somebody trying to solve your problem. On the other hand your fiddle contains way too much stuff than it needed. Please try to reduce your problem to the absolute minimum because your current fiddle contains too much and too messy code. Without a great amount of simplification I doubt that anyone would try to read/understand and help you with your issue. For a starter instead of including the mapping plugin directly, you can reference it from cdnjs.com. Commented Aug 7, 2013 at 5:42
  • updated the code in jsfiddle Commented Aug 7, 2013 at 5:46
  • jsFiddle updated to not use inline mapping plug-in Commented Aug 7, 2013 at 6:10
  • I think you problem is the getFullKey function and of course your base mapping in the two nested foreach loop. For example if I add: l_row += "<td class='seRow'><span data-bind='text: $root[" + index + "]." + key + "'/><textarea rows='1' data-bind='value: $root[" + index + "]." + key + "' /></td>"; The first row will display the right values Commented Aug 7, 2013 at 6:45

1 Answer 1

1

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);
                }
            }
        },
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.