我想简单地将计算的 observable 添加到映射的对象。我想避免添加父视图模型。我尝试使用映射选项创建,但这似乎不起作用。
$.getJSON(apiUrl, function (data) {
var mappingOptions = {
// overriding the default creation / initialization code
create: function (options) {
ko.mapping.fromJS(options.data, {}, this);
// setup the computed binding
this.FullNameComputed= ko.computed(function () {
return this.FirstName() + this.LastName();
}, this);
}
};
ko.mapping.fromJS(data, mappingOptions, self.Users);
}