As I dig deeper into the concepts of knockout.js, I’m having difficulties to understand why I can’t tell a ko.observable how to parse/write its value like so:
dateValue = ko.observable({
read: function (dateString) {
/*convert a date string to an object here for internal storage*/
return Globalize.parseDate(dateString, 'd');
},
write: function (dateObj) {
/*format a date object for output*/
return Globalize.formatDate(dateObj, 'd');
}
})
I’m aware that ko.computed’s exist for this purpose, but they still require me to keep a “shadow” observable where the result of read() needs to be written to.
Write an extender to add a
formattedcomputed observable so you may read or write formatted values while still giving you access to the “raw” unformatted value.