How knockout ko.mapping.fromJs() handles the $ref

2019-08-30 19:23发布

问题:

The Json which came from the server contains some refs like this:

[
    {
        id:"1",
        name:"Mehran",
        supervisor:
        {
            id:"2",
            name: "Ayaz",
            supervisor: null
        }
    },
    {
        id:"3",
        name:"Ramin",
        supervisor: {$ref="1"}
    }
]

How does knockout mapping's fromJS or fromJson interpret this?

回答1:

JSON in your example is invalid. It is unexpected token {$ref="1"}.
Argument for fromJS function should be the valid JSON
and argument for function fromJson should be a string representation of valid JSON value.

Knockout will throw exeption in both cases. You should pass full object instead of reference