使用 ko.mapping.fromJS() 创建的可观察数组具有可观察元素。我怎样才能让它的行为更像 ko.observableArray() ,其中数组是可观察的,但数组的元素不是?谢谢。
最佳答案
您可以使用映射插件创建选项。来自 ko 文档 (http://knockoutjs.com/documentation/plugins-mapping.html):
// The data to be mapped
var data = {
name: 'Graham',
children: [
{ id : 1, name : 'Lisa' }
]
};
// Your custom data model
var myChildModel = function (data) {
this.id = data.id;
this.name = data.name;
};
var mapping = {
'children': {
create: function(options) {
return new myChildModel(options.data);
}
}
};
var viewModel = ko.mapping.fromJS(data, mapping);
在此示例中,“children”数组将像可观察数组一样映射,其中属性不是可观察的。
关于javascript - 使通过 ko.mapping.fromJS 创建的可观察数组没有可观察元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146041/