I have the following javascript that does my knockout binding.
var context = this;
var viewModel = {
lineitems: [{
quantity: ko.observable(1),
title: 'bar',
description: 'foo',
price: ko.observable(10),
total: ko.observable(10),
formattedTotal: ko.computed({
read: function () {
return '$' + this.price().toFixed(2);
},
write: function (value) {
value = parseFloat(value.replace(/[^\.\d]/g, ""));
this.price(isNaN(value) ? 0 : value);
}
})
}]
};
ko.applyBindings(viewModel);
Which binds as expected, however when I apply the formattedTotal, I get the following javascript error.
Uncaught TypeError: Object [object global] has no method 'price'
I've tried a few changes to the syntax, but I can't seem to get it right, where am I going wrong?