【发布时间】:2012-05-20 18:02:58
【问题描述】:
有没有办法动态替换 ko.computed 中正在写入和读取的字段?例如,使用这个函数,我想用我可以传入的变量字段名替换 self.JobStartDate:
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
}
【问题讨论】:
标签: data-binding knockout.js computed-observable