我正在尝试使用 ko.mapping 插件创建函数将计算的 observable 添加到分层视图模型中。但该函数仅适用于顶级“子”数组!代码如下:
Children:[{
Name: "Page 1",
Children: [ {
Name: "Page 1 > 1",
Children: [{
Name: "Page 1 > 1 > 1",
Children: []
}
,{
Name: "Page 1 > 1 > 2",
Children: []
}
]
}
]
},
{
Name: "Page 2",
Children: [
{
Name: "Page 16",
Children: []
}
]
},
{
Name: "Page 3",
Children: [
{
Name: "aaaaa",
Children: []
}
]
我的功能如下
var computed = function (data) {
var self = this;
ko.mapping.fromJS(data, {}, self);
self.Uri = ko.computed(function () {
console.log(self.Name());
return self.Name() + " Something";
});
};
var mapping = {
'Children': {
create: function (o) {
return new computed(o.data);
}
}
};
ko.mapping.fromJS(data, mapping, vm);
有没有办法让“计算”函数在所有“子”数组上运行?谢谢。