Skip to content

Commit 988d092

Browse files
author
profreelancer222
committed
test
1 parent 6fd9319 commit 988d092

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

src/helpers.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
import { isObject } from './util'
22

3+
/**
4+
* Reduce the code which written in Vue.js for committing the mutation
5+
* @param {String} [namespace] - Module's namespace
6+
* @param {Object|Array} mutations # Object's item can be a function which accept `commit` function as the first param, it can accept another params. You can commit mutation and do any other things in this function. specially, You need to pass anthor params from the mapped function.
7+
* @return {Object}
8+
*/
9+
export const mapMutations = normalizeNamespace((namespace, mutations) => {
10+
const res = {}
11+
if (__DEV__ && !isValidMap(mutations)) {
12+
console.error('[vuex] mapMutations: mapper parameter must be either an Array or an Object')
13+
}
14+
normalizeMap(mutations).forEach(({ key, val }) => {
15+
res[key] = function mappedMutation (...args) {
16+
// Get the commit method from store
17+
let commit = this.$store.commit
18+
if (namespace) {
19+
const module = getModuleByNamespace(this.$store, 'mapMutations', namespace)
20+
if (!module) {
21+
return
22+
}
23+
commit = module.context.commit
24+
}
25+
return typeof val === 'function'
26+
? val.apply(this, [commit].concat(args))
27+
: commit.apply(this.$store, [val].concat(args))
28+
}
29+
})
30+
return res
31+
})
32+
333
/**
434
* Reduce the code which written in Vue.js for getting the state.
535
* @param {String} [namespace] - Module's namespace
@@ -33,35 +63,7 @@ export const mapState = normalizeNamespace((namespace, states) => {
3363
return res
3464
})
3565

36-
/**
37-
* Reduce the code which written in Vue.js for committing the mutation
38-
* @param {String} [namespace] - Module's namespace
39-
* @param {Object|Array} mutations # Object's item can be a function which accept `commit` function as the first param, it can accept another params. You can commit mutation and do any other things in this function. specially, You need to pass anthor params from the mapped function.
40-
* @return {Object}
41-
*/
42-
export const mapMutations = normalizeNamespace((namespace, mutations) => {
43-
const res = {}
44-
if (__DEV__ && !isValidMap(mutations)) {
45-
console.error('[vuex] mapMutations: mapper parameter must be either an Array or an Object')
46-
}
47-
normalizeMap(mutations).forEach(({ key, val }) => {
48-
res[key] = function mappedMutation (...args) {
49-
// Get the commit method from store
50-
let commit = this.$store.commit
51-
if (namespace) {
52-
const module = getModuleByNamespace(this.$store, 'mapMutations', namespace)
53-
if (!module) {
54-
return
55-
}
56-
commit = module.context.commit
57-
}
58-
return typeof val === 'function'
59-
? val.apply(this, [commit].concat(args))
60-
: commit.apply(this.$store, [val].concat(args))
61-
}
62-
})
63-
return res
64-
})
66+
6567

6668
/**
6769
* Reduce the code which written in Vue.js for getting the getters

0 commit comments

Comments
 (0)