|
@@ -1,3 +1,5 @@
|
|
|
|
+import { isObject } from './util'
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Reduce the code which written in Vue.js for getting the state.
|
|
* Reduce the code which written in Vue.js for getting the state.
|
|
* @param {String} [namespace] - Module's namespace
|
|
* @param {String} [namespace] - Module's namespace
|
|
@@ -6,6 +8,9 @@
|
|
*/
|
|
*/
|
|
export const mapState = normalizeNamespace((namespace, states) => {
|
|
export const mapState = normalizeNamespace((namespace, states) => {
|
|
const res = {}
|
|
const res = {}
|
|
|
|
+ if (process.env.NODE_ENV !== 'production' && !isValidMap(states)) {
|
|
|
|
+ console.error('[vuex] states parameter must be either an Array/Object')
|
|
|
|
+ }
|
|
normalizeMap(states).forEach(({ key, val }) => {
|
|
normalizeMap(states).forEach(({ key, val }) => {
|
|
res[key] = function mappedState () {
|
|
res[key] = function mappedState () {
|
|
let state = this.$store.state
|
|
let state = this.$store.state
|
|
@@ -36,6 +41,9 @@ export const mapState = normalizeNamespace((namespace, states) => {
|
|
*/
|
|
*/
|
|
export const mapMutations = normalizeNamespace((namespace, mutations) => {
|
|
export const mapMutations = normalizeNamespace((namespace, mutations) => {
|
|
const res = {}
|
|
const res = {}
|
|
|
|
+ if (process.env.NODE_ENV !== 'production' && !isValidMap(mutations)) {
|
|
|
|
+ console.error('[vuex] mutations parameter must be either an Array/Object')
|
|
|
|
+ }
|
|
normalizeMap(mutations).forEach(({ key, val }) => {
|
|
normalizeMap(mutations).forEach(({ key, val }) => {
|
|
res[key] = function mappedMutation (...args) {
|
|
res[key] = function mappedMutation (...args) {
|
|
// Get the commit method from store
|
|
// Get the commit method from store
|
|
@@ -63,6 +71,9 @@ export const mapMutations = normalizeNamespace((namespace, mutations) => {
|
|
*/
|
|
*/
|
|
export const mapGetters = normalizeNamespace((namespace, getters) => {
|
|
export const mapGetters = normalizeNamespace((namespace, getters) => {
|
|
const res = {}
|
|
const res = {}
|
|
|
|
+ if (process.env.NODE_ENV !== 'production' && !isValidMap(getters)) {
|
|
|
|
+ console.error('[vuex] getters parameter must be either an Array/Object')
|
|
|
|
+ }
|
|
normalizeMap(getters).forEach(({ key, val }) => {
|
|
normalizeMap(getters).forEach(({ key, val }) => {
|
|
// The namespace has been mutated by normalizeNamespace
|
|
// The namespace has been mutated by normalizeNamespace
|
|
val = namespace + val
|
|
val = namespace + val
|
|
@@ -90,6 +101,9 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
|
|
*/
|
|
*/
|
|
export const mapActions = normalizeNamespace((namespace, actions) => {
|
|
export const mapActions = normalizeNamespace((namespace, actions) => {
|
|
const res = {}
|
|
const res = {}
|
|
|
|
+ if (process.env.NODE_ENV !== 'production' && !isValidMap(actions)) {
|
|
|
|
+ console.error('[vuex] actions parameter must be either an Array/Object')
|
|
|
|
+ }
|
|
normalizeMap(actions).forEach(({ key, val }) => {
|
|
normalizeMap(actions).forEach(({ key, val }) => {
|
|
res[key] = function mappedAction (...args) {
|
|
res[key] = function mappedAction (...args) {
|
|
// get dispatch function from store
|
|
// get dispatch function from store
|
|
@@ -129,11 +143,23 @@ export const createNamespacedHelpers = (namespace) => ({
|
|
* @return {Object}
|
|
* @return {Object}
|
|
*/
|
|
*/
|
|
function normalizeMap (map) {
|
|
function normalizeMap (map) {
|
|
|
|
+ if (!isValidMap(map)) {
|
|
|
|
+ return []
|
|
|
|
+ }
|
|
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] }))
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Validate whether given map is valid or not
|
|
|
|
+ * @param {*} map
|
|
|
|
+ * @return {Boolean}
|
|
|
|
+ */
|
|
|
|
+function isValidMap (map) {
|
|
|
|
+ return Array.isArray(map) || isObject(map)
|
|
|
|
+}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
|
|
* Return a function expect two param contains namespace and map. it will normalize the namespace and then the param's function will handle the new namespace and the map.
|
|
* @param {Function} fn
|
|
* @param {Function} fn
|