-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuexHelper.js
34 lines (32 loc) · 1.04 KB
/
vuexHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import lodash from 'lodash';
import {createNamespacedHelpers} from 'vuex';
function createMutations(initialState) {
return Object.keys(initialState).reduce((obj, key) => {
obj[`${key}.set`] = (state, value) => {
if (initialState[key] === null || lodash.isPlainObject(initialState[key])) {
state[key] = Object.assign({}, state[key], lodash.cloneDeep(value));
} else {
state[key] = value;
}
};
return obj;
}, {});
}
export default function createNamespacedStore(name, {state: initialState, mutations = {}, actions = {}, getters = {}}) {
const {mapMutations, mapState, mapActions, mapGetters} = createNamespacedHelpers(name);
const _mutations = {
...createMutations(initialState),
...mutations
};
return {
namespaced: true,
state: lodash.cloneDeep(initialState),
getters,
actions,
mutations: _mutations,
mapState,
mapMutations,
mapGetters,
mapActions
};
}