-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New module option namespace
#380
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
Comments
I like this. One thing about nested modules with prefix - should it become |
definitely |
Because I thought if we want to register a root level mutation, we can clear parent prefix by overwriting. |
But the is would mean that
Could that be a problem, e.g. for third-party modules? |
I think parent should not add prefix in this case, like
Yes, I think this is developer's responsibility. We can select which the strategy of namespacing. This is the reason that I propose this prefix option as optional. In addition, I think #381 would solve this problem because it can access module getters etc. without concern of prefixes.
This is the point that we should think carefully. The problematic situation would be:
e.g. following code: // userland
import { pluginModule, plugin } from 'some-plugin'
export default new Vuex.Store({
modules: {
// registered under prefixed module
libs: {
prefix: 'libs/',
modules: {
somePlugin: pluginModule
}
}
},
plugins: [plugin]
})
// in plugin
export function plugin (store) {
someObject.on((data) => {
// this is failed since 'pluginEvent' action
// is registered as 'libs/pluginEvent'
store.dispatch('pluginEvent', data)
})
} I'm not sure that the solutions of this but here is some idea:
Any thoughts about this? |
One more thing, I think it should resolve prefixes of I'm currently thinking about providing export default {
prefix: 'prefix/',
actions: {
someAction ({ dispatch, commit, getters, rootGetters }) {
getters.someGetter // -> 'prefix/someGetter'
rootGetters.someGetter // -> 'someGetter'
dispatch('someOtherAction') // -> 'prefix/someOtherAction'
dispatch('someOtherAction', null, { root: true }) // -> 'someOtherAction'
commit('someMutation') // -> 'prefix/someMutation'
commit('someMutation', null, { root: true }) // -> 'someMutation'
}
}
} |
@ktsn Excellent proposal. I was thinking of solutions to these situations and this solution would be perfect. |
For the prefix to match my current "getters, actions and mutations" naming patterns, it would have to be applied differently on each case...
If |
To cover @jbruni's use case, I think we should change the option name to If string value is specified, it behaves as same as the original proposal. export default {
namespace (type, category) {
// 1st argument will be getters etc type before namespacing
// 2nd argument will be 'getter', 'action' or 'mutation'
// users must return namespaced name
return 'user/' + type // same as `namespace: 'user/'`
}
} |
I noticed that it is difficult to implement namespaced getters I described on this comment if the namespace option accepts function. Because we cannot predict the result of the namespace function. We can proxy to the getters in the same module but cannot proxy to the one in the other modules with same namespace. Following is the problematic case. export default {
namespace (type) {
return 'prefix' + type[0].toUpperCase() + type.slice(1)
},
modules: {
nestedA: {
getters: {
// prefixGetterA
getterA: () => 1
}
},
nestedB: {
getters: {
// prefixGetterB
getterB: () => 2
},
actions: {
someAction ({ getters, rootGetters }) {
getters.getterB // -> 2
getters.getterA // -> undefined
rootGetters.prefixGetterA // -> 1
}
}
}
}
} If we can use So I think we should implement |
@ktsn how can we use
But definitely would be better having |
I believe #381 covers that. |
Merged #420 :) |
Hi, is this already working in production?Cause I tied adding the 'namespace' option to my module, and I still get the 'duplicate xxx key' error and and using '"vuex": "^2.1.2"' |
Yes, this is working. Please direct usage questions to forum.vuejs.org |
This proposal is come up from the discussion in #359.
Summary of proposal
Modules will have
namespace
option that expects string value. All getters, actions and mutations types in the module are prefixed by the given value.Namespace is inherited to child modules implicitly and we can also nest namespace by declaring
namespace
option on the child modules.Also, this option can be more useful with
subStore
option that is another new module option proposal #381.Why is this worth adding?
Currently Vuex does not auto-namespace getters, actions and mutations even though they are defined in some nested modules, the namespacing is handled by developers. This design is reasonable because there are some cases that the auto-namespacing will suffer developers as discussed in #236 .
However, I think it would be useful that we have optional auto-namespacing because we probably want to prefix all getters, actions and mutations type in the same module in most cases.
Examples
The text was updated successfully, but these errors were encountered: