我使用 ko.mapping.fromJS 在从服务器获取数据后更新我的ViewModel。但这对我来说是一个问题。
在ko.mapping.fromJS之后,我获得了包含更新属性的View。但每次使用新数据重新创建属性时,都不会更新现有属性。
如何操作以获取ViewModel的更新属性,而不是使用新数据重新创建属性?
答案 0 :(得分:0)
如mapping plugin documentation中所述,
Let’s say you have a JavaScript object that looks like this:
var data = {
name: 'Scot',
children: [
{ id : 1, name : 'Alicw' }
]
}
You can map this to a view model without any problems:
var viewModel = ko.mapping.fromJS(data);
Now, let’s say the data is updated to be without any typos:
var data = {
name: 'Scott',
children: [
{ id : 1, name : 'Alice' }
]
}
Two things have happened here: name was changed from Scot to Scott and children[0].name was changed from Alicw to the typo-free Alice. You can update viewModel based on this new data:
ko.mapping.fromJS(data, viewModel);
最后一个字符串显示了如何使用新数据更新viewModel。