mutations.js
1.81 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import * as types from './mutations_types'
module.exports = {
[types.CLEAR_LOCAL](state) {
state.cartList.forEach(function(item) {
item.num = 0;
});
state.cartList = [];
localStorage.removeItem('vuex_cart');
},
[types.UPDATE_LOCAL](state) {
localStorage.setItem('vuex_cart', JSON.stringify(state.cartList));
},
[types.UPDATE_CUR_SHOP_STATUS](state, {
index = -1
}) {
state.curIndex = index;
},
[types.DELETE_DB](state) {
if (state.curIndex >= 0) {
state.cartList[state.curIndex].num = 0;
state.cartList.splice(state.curIndex, 1);
}
},
[types.CREATE_DB](state, shop) {
// console.log('mu create');
state.cartList.push(shop);
},
[types.ADD_DB](state) {
// console.log('mu add id:' + state.curIndex);
state.cartList[state.curIndex].num = parseInt(state.cartList[state.curIndex].num);
state.cartList[state.curIndex].num++;
},
[types.REDUCE_DB](state) {
// console.log('mu reduce');
state.cartList[state.curIndex].num = parseInt(state.cartList[state.curIndex].num);
state.cartList[state.curIndex].num--;
// console.log(state.cartList[state.curIndex].num);
if (state.cartList[state.curIndex].num == 0) {
state.cartList.splice(state.curIndex, 1);
}
},
[types.CHECK_DB](state, {
id
}) {
// console.log('mu check id :' + id);
// console.log(state.cartList);
state.curIndex = -1;
var list = state.cartList;
if (list.length) {
for (var i = 0; i < list.length; i++) {
if (list[i].id == id) {
state.curIndex = i;
break;
}
}
}
}
};