What I'm trying to do is keep two text box's math in sync. The fields I'm working with is subtotal, taxTotal, tax rate, and sale total. What I want to happen is:
- If the taxRate is updated by the user, then the taxTotal will update with the correct amount (subTotal * taxRate).
- If the taxTotal is updated by the user, then the taxRate should be updated with the correct amount (taxTotal / subTotal * 100)
I didn't get very far with this, and I think it's because I keep thinking in terms of properties with backing fields (like in C#) and I'm having trouble trying to figure out the logic to keep everything bound and observable within the knockout framework (managing dirty states etc).
Does anyone have a solution for this? As far as I got led me to using pureComputed instead of computed. e.g.
this.subTotal = ko.observable(0.00);
this.taxRate = ko.pureComputed({
read: function(){
},
write: function(){
},
owner: this
});
this.taxTotal = ko.pureComputed({
read: function(){
},
write: function(){
},
owner: this
});
this.saleTotal = ko.computed(function(){
return this.subTotal ()+ this.taxTotal();
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…