Created
March 28, 2017 16:42
-
-
Save wochap/16574590fa92d00e50a53a99a4c44e98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const mapState = normalizeNamespace((namespace, states, alias = []) => { | |
const res = {} | |
normalizeMap(states).forEach(({ key, val }, index) => { | |
res[alias[index] || key] = function mappedState () { | |
let state = this.$store.state | |
let getters = this.$store.getters | |
if (namespace) { | |
const module = getModuleByNamespace(this.$store, 'mapState', namespace) | |
if (!module) { | |
return | |
} | |
state = module.context.state | |
getters = module.context.getters | |
} | |
return typeof val === 'function' | |
? val.call(this, state, getters) | |
: state[val] | |
} | |
// mark vuex getter for devtools | |
res[alias[index] || key].vuex = true | |
}) | |
return res | |
}) | |
export const mapMutations = normalizeNamespace((namespace, mutations, alias = []) => { | |
const res = {} | |
normalizeMap(mutations).forEach(({ key, val }, index) => { | |
val = namespace + val | |
res[alias[index] || key] = function mappedMutation (...args) { | |
if (namespace && !getModuleByNamespace(this.$store, 'mapMutations', namespace)) { | |
return | |
} | |
return this.$store.commit.apply(this.$store, [val].concat(args)) | |
} | |
}) | |
return res | |
}) | |
export const mapGetters = normalizeNamespace((namespace, getters, alias = []) => { | |
const res = {} | |
normalizeMap(getters).forEach(({ key, val }, index) => { | |
val = namespace + val | |
res[alias[index] || key] = function mappedGetter () { | |
if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) { | |
return | |
} | |
if (!(val in this.$store.getters)) { | |
console.error(`[vuex] unknown getter: ${val}`) | |
return | |
} | |
return this.$store.getters[val] | |
} | |
// mark vuex getter for devtools | |
res[alias[index] || key].vuex = true | |
}) | |
return res | |
}) | |
export const mapActions = normalizeNamespace((namespace, actions, alias = []) => { | |
const res = {} | |
normalizeMap(actions).forEach(({ key, val }, index) => { | |
val = namespace + val | |
res[alias[index] || key] = function mappedAction (...args) { | |
if (namespace && !getModuleByNamespace(this.$store, 'mapActions', namespace)) { | |
return | |
} | |
return this.$store.dispatch.apply(this.$store, [val].concat(args)) | |
} | |
}) | |
return res | |
}) | |
function normalizeMap (map) { | |
return Array.isArray(map) | |
? map.map(key => ({ key, val: key })) | |
: Object.keys(map).map(key => ({ key, val: map[key] })) | |
} | |
function normalizeNamespace (fn) { | |
return (namespace, map, alias) => { | |
if (typeof namespace !== 'string') { | |
map = namespace | |
namespace = '' | |
} else if (namespace.charAt(namespace.length - 1) !== '/') { | |
namespace += '/' | |
} | |
return fn(namespace, map, alias) | |
} | |
} | |
function getModuleByNamespace (store, helper, namespace) { | |
const module = store._modulesNamespaceMap[namespace] | |
if (!module) { | |
console.error(`[vuex] module namespace not found in ${helper}(): ${namespace}`) | |
} | |
return module | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment