有没有办法动态替换ko.computed中写入和读取的字段?例如,使用此函数,我想将self.JobStartDate替换为我可以传入的变量fieldname:
function Job(data) {
var self = this;
ko.mapping.fromJS(data, {}, this);
var computedDateFn = {
read: function() {
return formatDate(ko.utils.unwrapObservable(self.JobStartDate), true);
},
write: function(value) {
var jsonDate = "/Date(" + Date.parse(value).getTime();
self.JobStartDate(jsonDate);
}
}
this.formattedStartDate = ko.computed(computedDateFn);
this.formattedEndDate = ko.computed(computedDateFn); // this guy would need the field it writes to/reads from to be self.JobEndDate
}
答案 0 :(得分:1)
如果你使calculateDateFn成为一个接受你的fieldname为string并返回定义你的计算observable的对象的函数,你可以使用这样的数组表示法......