complex nested objects are not reactive
See original GitHub issueThis is my first post here, if I’m not mistaken, so let me say thank you for an amazing framework which inspires us all!
I maintain a package called contextable.js. It uses schema-based objects to describe application’s data model and its primary focus is to simplify server-side and client-side data validation. I’ve been using it in Express.js and GraphQL resolvers for now, but recently I also started using it in Vue.js through vue-contextable plugin.
I’m not sure if this is a bug report or a feature request but I kindly ask for directions on how to handle v-model
reactivity in a complex object where arrays of class instances are used. I suspect that Vue.js can’t track changes on arrays of class instance objects though I’m not sure what would be the difference in this case compare to an Object
. I’ve created an example here so you can try it yourself.
Vue.js version: 2.1.0
Reproduction Link: https://github.com/xpepermint/vue-contextable-example
Steps to reproduce: Run the example and play with the provided form.
What is Expected?: The user
model should be updated when input changes.
What is actually happening?: The model is not updated.
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (2 by maintainers)
Top GitHub Comments
This solved my issue. I will show both to help show what he’s talking about:
This is problematic:
That is problematic because
billing_city
andbilling_region
were not keys onmodifiedAccount
, so they were basically not being watched, is how I interpret that.This is a solution to that problem:
This overwrites
modifiedAccount
with a copy of itself plus the new keys spread in, which will be familiar to many people, as it is a common immutable approach.Without testing, I am pretty sure
this.modifiedAccount.billing_city = newLocation.city;
would trigger a re-render after you did the above solution because Vue would then be watching those keys.EDIT: I just tested and the component did re-render after I did the above solution and then did:
Thanks @yyx990803. I went through the documentation again and I found this paragraph:
This is exactly what the problem is. I’ll close this issue since I see how to solve it now. Thanks!