In my attempt to create a tool with 3 inputs that are interdependent - "Earn %", "Earn $" and "Own Price".
Initially, the default value for "Earn percentage" is set at "10", making the initial calculation function as intended. Changing this one value automatically adjusts the other two because there are no circular references: https://jsfiddle.net/2m971kur/2/
const app = new Vue({
el: '#app',
data: {
exVAT: 1500,
retailPrice: 2900,
earnPercentage: 10
},
computed: {
incVAT() {
return this.exVAT * 1.25;
},
ownPrice() {
return this.exVAT + (this.exVAT * (this.earnPercentage / 100));
},
earnAmount() {
return this.ownPrice - this.exVAT;
}
}
})
However...
When attempting to introduce circular references, my code encounters issues:
https://jsfiddle.net/xrwykvg5/
const app = new Vue({
el: '#app',
data: {
exVAT: 1500,
retailPrice: 2900,
earnPercentage: 10,
ownPrice: 0,
earnAmount: 0
},
watch: {
earnPercentage() {
this.earnAmount = this.exVAT * (this.earnPercentage / 100);
this.ownPrice = this.exVAT + this.earnPercentage;
},
ownPrice() {
this.earnAmount = this.ownPrice - this.exVAT;
this.earnPercentage = 100 / (this.ownPrice / this.exVAT);
},
earnAmount() {
this.ownPrice = this.exVAT + this.earnAmount;
this.earnPercentage = (this.ownPrice / this.exVAT) * 100;
}
}
})
How can I resolve this issue?
While the example was created using Vue.js, showcasing a simple demonstration of the problem, my actual code is in Angular 2.