Illustration of the issue: https://jsfiddle.net/hxv5bLqt/
In my data structure, there are two levels (arrays). The top level represents "parts", and each "part" contains a set of "items". Additionally, there is a variable that stores the total value of all items. The functionality needed involves recalculating this total whenever a user modifies any value field.
Although I have implemented a solution for this requirement, I find it unsatisfactory as it involves manually tracking where the "this.recompute()" method should be called. Utilizing watch functions or computed properties would offer a cleaner and more reactive approach.
Unfortunately, implementing a "watch" or a "computed property" to monitor changes within objects nested in arrays has proven challenging.
An ideal watch function that I envisioned working would look something like:
watch: {
'parts.X.items.Y.value': function(oldValue, newValue) {
this.recompute()
}
}
The variables "X" and "Y" represent placeholders for indicating any positions within the arrays that we want to observe.
This setup would trigger the watch every time a user interacts with an item by removing it or editing its value. However, this approach does not yield the expected results, leading me back to manually calling "this.recompute()" at specific points in the code:
onClickRemoveItem(event, part, item) {
...
this.recompute() // RECOMPUTE HERE.
},
onKeyupItemValue(event) {
this.recompute() // RECOMPUTE HERE.
},
I believe relying on explicit function calls like this is not an elegant solution, especially when dealing with numerous potential call sites for "this.recompute()". Is there a more efficient and elegant way to address this issue?