|
@@ -1,28 +1,41 @@
|
|
-export function mapState (states) {
|
|
|
|
|
|
+export const mapState = normalizeNamespace((namespace, states) => {
|
|
const res = {}
|
|
const res = {}
|
|
normalizeMap(states).forEach(({ key, val }) => {
|
|
normalizeMap(states).forEach(({ key, val }) => {
|
|
res[key] = function mappedState () {
|
|
res[key] = function mappedState () {
|
|
|
|
+ let state = this.$store.state
|
|
|
|
+ let getters = this.$store.getters
|
|
|
|
+ if (namespace) {
|
|
|
|
+ const module = this.$store._modulesNamespaceMap[namespace]
|
|
|
|
+ if (!module) {
|
|
|
|
+ warnNamespace('mapState', namespace)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ state = module.state
|
|
|
|
+ getters = module.context.getters
|
|
|
|
+ }
|
|
return typeof val === 'function'
|
|
return typeof val === 'function'
|
|
- ? val.call(this, this.$store.state, this.$store.getters)
|
|
|
|
- : this.$store.state[val]
|
|
|
|
|
|
+ ? val.call(this, state, getters)
|
|
|
|
+ : state[val]
|
|
}
|
|
}
|
|
})
|
|
})
|
|
return res
|
|
return res
|
|
-}
|
|
|
|
|
|
+})
|
|
|
|
|
|
-export function mapMutations (mutations) {
|
|
|
|
|
|
+export const mapMutations = normalizeNamespace((namespace, mutations) => {
|
|
const res = {}
|
|
const res = {}
|
|
normalizeMap(mutations).forEach(({ key, val }) => {
|
|
normalizeMap(mutations).forEach(({ key, val }) => {
|
|
|
|
+ val = namespace + val
|
|
res[key] = function mappedMutation (...args) {
|
|
res[key] = function mappedMutation (...args) {
|
|
return this.$store.commit.apply(this.$store, [val].concat(args))
|
|
return this.$store.commit.apply(this.$store, [val].concat(args))
|
|
}
|
|
}
|
|
})
|
|
})
|
|
return res
|
|
return res
|
|
-}
|
|
|
|
|
|
+})
|
|
|
|
|
|
-export function mapGetters (getters) {
|
|
|
|
|
|
+export const mapGetters = normalizeNamespace((namespace, getters) => {
|
|
const res = {}
|
|
const res = {}
|
|
normalizeMap(getters).forEach(({ key, val }) => {
|
|
normalizeMap(getters).forEach(({ key, val }) => {
|
|
|
|
+ val = namespace + val
|
|
res[key] = function mappedGetter () {
|
|
res[key] = function mappedGetter () {
|
|
if (!(val in this.$store.getters)) {
|
|
if (!(val in this.$store.getters)) {
|
|
console.error(`[vuex] unknown getter: ${val}`)
|
|
console.error(`[vuex] unknown getter: ${val}`)
|
|
@@ -31,20 +44,37 @@ export function mapGetters (getters) {
|
|
}
|
|
}
|
|
})
|
|
})
|
|
return res
|
|
return res
|
|
-}
|
|
|
|
|
|
+})
|
|
|
|
|
|
-export function mapActions (actions) {
|
|
|
|
|
|
+export const mapActions = normalizeNamespace((namespace, actions) => {
|
|
const res = {}
|
|
const res = {}
|
|
normalizeMap(actions).forEach(({ key, val }) => {
|
|
normalizeMap(actions).forEach(({ key, val }) => {
|
|
|
|
+ val = namespace + val
|
|
res[key] = function mappedAction (...args) {
|
|
res[key] = function mappedAction (...args) {
|
|
return this.$store.dispatch.apply(this.$store, [val].concat(args))
|
|
return this.$store.dispatch.apply(this.$store, [val].concat(args))
|
|
}
|
|
}
|
|
})
|
|
})
|
|
return res
|
|
return res
|
|
-}
|
|
|
|
|
|
+})
|
|
|
|
|
|
function normalizeMap (map) {
|
|
function normalizeMap (map) {
|
|
return Array.isArray(map)
|
|
return Array.isArray(map)
|
|
? map.map(key => ({ key, val: key }))
|
|
? map.map(key => ({ key, val: key }))
|
|
: Object.keys(map).map(key => ({ key, val: map[key] }))
|
|
: Object.keys(map).map(key => ({ key, val: map[key] }))
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+function normalizeNamespace (fn) {
|
|
|
|
+ return (namespace, map) => {
|
|
|
|
+ if (typeof namespace !== 'string') {
|
|
|
|
+ map = namespace
|
|
|
|
+ namespace = ''
|
|
|
|
+ } else if (namespace.charAt(namespace.length - 1) !== '/') {
|
|
|
|
+ namespace += '/'
|
|
|
|
+ }
|
|
|
|
+ return fn(namespace, map)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function warnNamespace (helper, namespace) {
|
|
|
|
+ console.error(`[vuex] module namespace not found in ${helper}(): ${namespace}`)
|
|
|
|
+}
|