Drew H
Drew H

Reputation: 4739

What is wrong with my object(regarding knockoutjs mapping plugin)

I'm pulling my hair out with this. I'm not sure what is going on. I've read all the tutorials on mapping but I'm obviously missing something.

{
        "address": "110",
        "city": "Durham",
        "id": 1,
        "name": "Keep",
        "persistent": true,
        "salesRep": "Me",
        "state": "NC",
        "user": {
            "email": "[email protected]",
            "id": 4,
            "name": "Test",
            "password": "test",
            "persistent": true
        }
    }

I've tried

ko.mapping.fromJSON(data);

and

ko.mapping.fromJS(data);

In my old code I ended up doing this.

    viewModel.customers(data);

But my JSON looked different. It didn't have a nested object and it also had brackets on the beginning and end making it an array. It seems this would be pretty basic. I'm not getting any errors at all. Thanks for the help.

EDIT

http://jsfiddle.net/gjemN/

Upvotes: 0

Views: 560

Answers (1)

RP Niemeyer
RP Niemeyer

Reputation: 114792

In your sample you are dealing with a JavaScript object and not JSON (string representation of it).

So, you would want to call ko.mapping.fromJS. If you are getting back an array of customers, then you could do:

ko.mapping.fromJS(data2, null, viewModel.customers)

Something like: http://jsfiddle.net/rniemeyer/BQe2z/

Upvotes: 3

Related Questions