Skip to content

Commit e4f59d4

Browse files
author
anthinkingcoder
committed
en doc
1 parent e8a1148 commit e4f59d4

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

README.md

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# vuex-loading
2-
简化vuex中异步请求,不得不维护loading state的问题,支持代理异步回调与promise
2+
Simplify vuex loading state management
33

44
## Installing
55

@@ -10,19 +10,19 @@ $ npm install vuex-loadings -s
1010
```
1111

1212
## Example
13-
14-
1513
```js
1614
//product.js
1715
import vxl from 'vuex-loadings'
1816
const api = {
17+
//callback
1918
listProduct (cb,errorCb) {
2019
fetch('/api/listProduct').then((result) => {
2120
cb(result)
2221
}).catch(error => {
2322
errorCb(error)
2423
})
2524
},
25+
//promise
2626
productDetail (id) {
2727
return fetch(`/api/productDetail?id=${id}`)
2828
}
@@ -38,23 +38,42 @@ const getters = {
3838
}
3939
}
4040

41-
//aopLoading(commit,loadingName, fn, isPromise)
42-
//有两种fn可以代理,第一种fn是一个promise,第二种fn是接受两个回调函数参数->成功回调和失败回调,设置isPromise可以选择指定方式代理。
4341
const actions = {
4442
listProduct ({{commit}}) {
43+
//proxy callback
4544
let request = vxl.aopLoading(commit, 'productsLoading',api.listProduct)
4645
request((result) => {
4746
commit('setProducts', result)
4847
}, error => {
4948
})
49+
//it equal
50+
//commit('setProductsLoading',true)
51+
//api.listProduct((result) => {
52+
// commit('setProductsLoading',false)
53+
// commit('setProducts', result)
54+
// }, error => {
55+
// commit('setProductsLoading',false)
56+
// })
5057
},
5158
productDetail ({{commit}}) {
59+
//proxy promise
5260
let request = vxl.aopLoading(commit, 'productDetail', api.productDetail,true)
5361
request(1).then((result) => {
5462
commit('setProducts', result)
5563
}).catch(error => {
5664
console.info(error)
5765
})
66+
67+
//it equal
68+
//commit('setProductDetailLoading',true)
69+
//api.productDetail(1)
70+
// .then(result => {
71+
// commit('setProductDetailLoading',false)
72+
// commit('setProducts', result)})
73+
// .catch(error => {
74+
// commit('setProductDetailLoading',false)
75+
// console.info(error)
76+
// })
5877
}
5978
}
6079

@@ -67,16 +86,22 @@ const mutations = {
6786
}
6887
}
6988

70-
//使用vxl.mixin将productsLoading相关操作注入state,getters,mutations中,这样我们不需要手写大堆的代码
89+
//it will be set loading state,loading getter,loading mutation for products,productDetail
90+
//default loading name is productsLoading,productDetailLoading
7191
vxl.mixin({state,getters,mutations}, ['products','productDetail'])
72-
//或者可以指定loading名字
92+
93+
//or custom loading state name
7394
vxl.mixin({state,getters,mutations}, [{'products':'productsLoading'},'productDetail'])
7495

75-
export {
76-
state,getters,actions,mutations
96+
export default {
97+
namespaced: true,
98+
state,
99+
getters,
100+
actions,
101+
mutations
77102
}
78103
```
79-
### 另一种方式
104+
### other way
80105

81106
```js
82107
import {aopLoading, mapLoadingMutations, mapLoadingState, mapLoadingGetters} from '../vuex-loading'
@@ -90,25 +115,30 @@ const api = {
90115
}
91116
}
92117
const state = {
93-
//为clues绑定loading,这种方法不支持自定义loading name,默认规则是clues -> cluesLoading
94118
...mapLoadingState({
95119
clues: {},
96120
})
121+
//it will be -> {
122+
// clues: {}
123+
// cluesLoading: {}
124+
//}
97125
}
98126

99127

100128
const getters = {
101129
clues(state) {
102130
return state.clues
103131
},
132+
133+
// set getter function for cluesLoading
104134
...mapLoadingGetters(['cluesLoading'])
105-
//或者是自定义getters名
135+
//or custom getter function name
106136
...mapLoadingGetters({'cluesLoading': 'getCluesLoading'})
107137
}
108138

109139
const actions = {
110140
listClue({commit}) {
111-
//同上
141+
//the same
112142
let request = aopLoading(commit, 'cluesLoading', marketApi.listClue)
113143
request(items => {
114144
commit('setClues', {items})
@@ -122,7 +152,7 @@ const mutations = {
122152
setClues(state, {items}) {
123153
state.clues = items
124154
},
125-
//绑定mutations方法
155+
//set mutation function for cluesLoading
126156
...mapLoadingMutations(['cluesLoading'])
127157
}
128158

0 commit comments

Comments
 (0)