Domanda

I am attempting to load a ViewModel property with mapped data on initial page load and then at a later time, append new JSON data to this mapped property. Following the docs here (http://knockoutjs.com/documentation/plugins-mapping.html), it would appear that all I need to do is call ko.mapping.fromJS(data, {}, someObject); to combine data and someObject. Can someone tell me why this would not work? Please see fiddle example below:

http://jsfiddle.net/m4uKe/

I would expect

{
  "Foo": [
    {
      "Property": "Value1"
    }
  ]
}

to become

{
  "Foo": [
    {
      "Property": "Value1"
    },
    {
      "Property": "Value2"
    }
  ]
}
È stato utile?

Soluzione

You should push an item to the observable array obtained from the first call to fromJS. So change this line:

ko.mapping.fromJS(data2, {}, self.Foo);

to this

self.Foo.push(ko.mapping.fromJS(data2));

Live example: http://jsfiddle.net/m4uKe/2/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top