-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested mutations #828
Comments
Have you considered using modules in store for this, I think you could accomplish what you are describing with it |
From the vuex docs: Within a child module mutation function, the said function cannot directly manipulate the root.state unless accessed via by dispatching an action which is way overkill for what nesting mutations could do. The modules ability to hold their own state is great but not always needed. By nesting you are simply grouping one modules mutations to reduce the overall code written. Name spacing each module and then using the If the a child module's mutation could gain access to the Where it would be great to get to is syntax like this: |
I'm not sure this is a good idea because we wouldn't able to extend the mutation syntax if we introduce this. (e.g. We cannot add #770 if we allow nested mutation declaration) I think this should be implemented as a mutation enhancer that is separated with Vuex. For example we can introduce mutations: nestedMutations({
phase: {
x: {
add (state) {
//...
},
remove (state) {
//...
}
}
}
}) The above code is equivalent with: mutations: {
'phase.x.add' (state) {
// ...
},
'phase.x.remove' (state) {
// ...
}
} |
@ktsn I agree with you. This enhancer could be published as a separate utility though, right? It woulnd't have to be done in core. So I vote to close this request. |
OK, I wrote the enhancer then and published it. I also have it working on a project I am working on. It was a great idea to abstract this from @ktsn : |
Great! |
I know this is closed, but i was hoping to maybe either re-open it or get some clearer insight as to why this is not possible. The vuex-nested-mutations package i wrote enables nested mutations, and cleans up code nicely and thus reduces the qty of code sent out to production. But... the IDE cannot autocomplete against the mutation object and the object itself is flattened by the vuexNestedMutations function. The inability to autocomplete results in typos and generally slows the developer down.. If nested mutations were written into the core: If you don't need that annoying mutation-types file that is one less file to maintain... i added a snapshot of a new small app i just built using the suggested constants method... nobody can agree that this is easy to read and thus quick and easy to maintain, this is after a couple of hours and it is already just a long terrible list only a computer is good are parsing: This is the same code but nested: https://ibb.co/jQjcxG so much more easier to understand at a mere glance! https://ibb.co/f1JZBb @ktsn You mentioned that if this was added to the core it would reduce the ability to extend the mutations sytanx in the future, but if the core reduced a mutation nested object to flattened array of camelCase keys sure everything remains the same. As will any testing.. ? Thus I would argue the benifit of increased dev time and reduction of typos was worth it. Plus developers are lazy and don't want to type if they don't have to.. ;) |
What problem does this feature solve?
Solves the duplication of mutation function names.
In the above, the string "phase" and "X" have to be written down for each function. This both increases the code base size and increases the time for development, this also is more prone to typo's.
What does the proposed API look like?
Now to call the "phase x add" the syntax is the std . spearated syntax:
this.$store.commit('phase.x.add')
This now allows grouping within modules where each nesting within a single module can directly affect its own state. The code qty is reduced and easier to navigate and easier to call from a
commit
.To call nested mutations within modules is also clear and simple:
this.$store.commit('board/phase.x.add')
The text was updated successfully, but these errors were encountered: