简体   繁体   English

JSON对象数组,用于淘汰具有可观察属性的可观察数组

[英]Array of JSON Objects to Knockout Observable Array With Observable Properties

My app has an ajax call that will return an array of JSON objects. 我的应用程序有一个ajax调用,它将返回一个JSON对象数组。

[
{"ID":2,"Name":"Name 1","CreatedOn":"/Date(1432892160000)/"},
{"ID":7,"Name":"Name 2","CreatedOn":"/Date(1432892160000)/"},
{"ID":8,"Name":"Name 3","CreatedOn":"/Date(1432892160000)/"},
{"ID":9,"Name":"Name 4","CreatedOn":"/Date(1432892160000)/"},
{"ID":10,"Name":"Name 5","CreatedOn":"/Date(1432854000000)/"}
]

I need to then assign these to a knockout observable array where the object properties are also observable. 然后,我需要将它们分配给可观察对象属性的可观察数组。

I can create the observable array without a problem. 我可以毫无问题地创建可观察数组。

viewModel.newArray= ko.observableArray([]);
viewModel.newArray(result.ReturnedObjects);

However I can't suss out how to push to the observable array and make the properties of each object observable. 但是,我无法解决如何推送到可观察数组并使每个对象的属性可观察的问题。

Use Knockout Mapping Plugin . 使用Knockout映射插件 Something like this should work 这样的事情应该工作

function vm(result){
  var self = this;
  self.items = ko.observableArray();
  ko.mapping.fromJS(result.ReturnedObjects,{},self.items)
  console.log(self.items()); //array with each object props as observables
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM