Is there a way to dynamically replace the field being written to and read from in the ko.computed? For example, with this function, I want to replace the self.JobStartDate with a variable fieldname that I could pass in:
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
}
If you make computedDateFn a function that accepts your fieldname as string and returns the object defining your computed observable, you can use array notation like this...
http://jsfiddle.net/bczengel/tMTCV/