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

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


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?


Solution

  • 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