I am having trouble with a computed property within dynamically populated array of objects. I have an array of customers which are filled dynamically by the user in the form. The user can add as many customers as he likes. Each customer has a price field calculated based on some other values in the object. Additionally, the price for each customer is based on some generic settings that apply to all customers.
Therefore I have a computed function which should update the price for each customer. The function starts like this:
this.booking.customers.forEach(function(customer) {
var price = 0;
self.booking.packages.forEach(function(package) {
... // based on the packages the price variable is changing
}
customer.price = price;
}
The calculation works - because if I open the vue devtools the value is suddenly applied, and then Vue also recognizes the dependencies (as soon as I change some value all prices adapt accordingly).
So my problem is: initially, Vue doesn't update the property correctly. As soon as I open devtools once, from that moment on it adapts accordingly.
Maybe I am not using computed properties in the right way, but using a method is also not what I need because I want the price value to change as soon as any dependency is changing.
Hope the problem is understandable. Thanks in advance for your help.
EDIT:
Here is a fiddle which maybe highlights the problem better. I think I am misusing computed values, but I have no idea how to tell Vue that the prices need to change whenenver some other values change. http://jsfiddle.net/6bw50j9d/2/
ANOTHER EDIT: I was able to solve my problem using deep watchers, I found this solution in another post. See Vue.js - How to properly watch for nested properties