I have settled into using this pattern to build Vue components that are editing content that is loaded into my Vuex store.
It feels like a lot of boilerplate to me so I'm not sure if I'm doing something wrong or un-Vue-like.
I load a copy of what's in the local store to local state so that the edits occur locally instead of modifying my state so that my state always represents what's in my database. This allows me to do things like compute dirty states for local components. It always just feels like the right thing to do.
My store's state only updates when data is returned from API calls to my backend database.
You can see here that in my components for each field I:
- create a field in my
data()
'smodel
. I nest insidemodel
to avoid name conflict with mymapState
properties. It also nicely segments model data from other UI state data I might need or want. - add a
load*
method to copy in data to the local model from themapState
property - call each
load*
method in both awatch
method for that property as well as themounted()
lifecycle hook.
In this example, it's not too bad but when I have a component that is editing 6 fields or so, it's a LOT of boilerplate that seems very repetitive.