trailing { root: true } object is verbose and spoils code style
dispatch('api/foo', {
bar: 'buz',
quz: 'qux',
}, { root: true }) // ugly trailing options object
// leading slash makes it look better
dispatch('/api/foo', {
bar: 'buz',
quz: 'qux',
});
I second this.
It's worse, when an action does not take a parameter, then we get
dispatch('api/get', null, { root: true })
instead of the much nicer
dispatch('/api/get')
While a great idea, technically this is a breaking change since previously it would be possible to name a module like this:; /module.
But who would do that? The answer to that question is probably "nobody", but the truth is "somebody, somewhere is actually doing that".
So this would be a good idea for when we overwhaul vuex, but I'd be hesitant to do this in a minor release.
Why not make this an opt-in feature to prevent affecting existing projects?
I've been thinking about this as well. An alternative approach would be rootCommit and rootDispatch functions exposed for actions in the same manner as they can access rootState and rootGetters, so instead of:
export const someAction ({ commit, dispatch }) {
commit('path/to/module/someMutation', null, { root: true })
dispatch('path/to/anotherModule/someAction', null, { root: true })
}
we could do:
export const fancierAction ({ rootCommit, rootDispatch }) {
rootCommit('path/to/module/someMutation')
rootDispatch('path/to/anotherModule/someAction')
}
I actually thought of doing this in my current project, but the wrappedActionHandler function is given inline here: https://github.com/vuejs/vuex/blob/dev/src/store.js#L411 so I guess it's not possible?
Does having a slash in the middle of the action name imply that it is to another namespace? If you just look for the slash in the middle of the action would that get around the concern of LinusBorg above?
action.indexOf('/') > 0
Forgive my ignorance of vuex if I am missing something.
A slash implies the action is to another namespace, but it doesn鈥檛 guarantee the user didn鈥檛 decide to use a leading slash in a module name for whatever reason.
Why not make this an opt-in feature to prevent affecting existing projects?
Also make the root character configurable :)
Was there any further discussion on this? I would love to see this.
rootDispatch makes so much sense!
Most helpful comment
I've been thinking about this as well. An alternative approach would be
rootCommitandrootDispatchfunctions exposed for actions in the same manner as they can accessrootStateandrootGetters, so instead of:we could do:
I actually thought of doing this in my current project, but the
wrappedActionHandlerfunction is given inline here: https://github.com/vuejs/vuex/blob/dev/src/store.js#L411 so I guess it's not possible?