Skip to content

Commit 043b31d

Browse files
KingMarioyyx990803
authored andcommitted
Tips for action arrangement (vuejs#191)
1 parent a963ac4 commit 043b31d

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

docs/en/actions.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const vm = new Vue({
6767
})
6868
```
6969

70-
What the above code does is bind the raw `incrementBy` action to the component's store instance, and expose it on the component as an instance method, `vm.incrementBy`. Any arguments passed to `vm.incrementBy` will be passed to the raw action function after the first argument which is the store, so calling:
70+
What the above code does is to bind the raw `incrementBy` action to the component's store instance, and expose it on the component as an instance method, `vm.incrementBy`. Any arguments passed to `vm.incrementBy` will be passed to the raw action function after the first argument which is the store, so calling:
7171

7272
``` js
7373
vm.incrementBy(1)
@@ -132,3 +132,68 @@ const vm = new Vue({
132132
}
133133
})
134134
```
135+
136+
### Arrange Actions in Modules
137+
138+
Normally in large applications, actions should be arranged in groups/modules for different purposes. For example, userActions module deals with user registration, login, logout, and so on, while shoppingCartActions module deals with other tasks for shopping.
139+
140+
Modularization is more convenient for different components to import only required actions.
141+
142+
You may import action module into action module for reusability.
143+
144+
```javascript
145+
// errorActions.js
146+
export const setError = ({dispatch}, error) => {
147+
dispatch('SET_ERROR', error)
148+
}
149+
export const showError = ({dispatch}) => {
150+
dispatch('SET_ERROR_VISIBLE', true)
151+
}
152+
export const hideError = ({dispatch}) => {
153+
dispatch('SET_ERROR_VISIBLE', false)
154+
}
155+
```
156+
157+
```javascript
158+
// userActions.js
159+
import {setError, showError} from './errorActions'
160+
161+
export const login = ({dispatch}, username, password) => {
162+
if (username && password) {
163+
doLogin(username, password).done(res => {
164+
dispatch('SET_USERNAME', res.username)
165+
dispatch('SET_LOGGED_IN', true)
166+
dispatch('SET_USER_INFO', res)
167+
}).fail(error => {
168+
dispatch('SET_INVALID_LOGIN')
169+
setError({dispatch}, error)
170+
showError({dispatch})
171+
})
172+
}
173+
}
174+
175+
```
176+
177+
While calling actions from one another module, or while calling another action in the same module, remember that actions take a store instance as its first argument, so the action called inside another action should be passed through the first argument for the caller.
178+
179+
If you write the action with ES6 destructuring style, make sure that the first argument of the caller action covers all the properties and methods of both actions. For example, only *dispatch* is used in the caller action and *state*, *watch* are used in the called action, all the *dispatch*, *state* and *watch* should be presented in the caller first formal argument like this:
180+
181+
```javascript
182+
import {callee} from './anotherActionModule'
183+
184+
export const caller = ({dispatch, state, watch}) {
185+
dispatch('MUTATION_1')
186+
callee({state, watch})
187+
}
188+
```
189+
190+
Otherwise, you should use the old-fationed function syntax:
191+
192+
```javascript
193+
import {callee} from './anotherActionModule'
194+
195+
export const caller = (store) {
196+
store.dispatch('MUTATION_1')
197+
callee(store)
198+
}
199+
```

0 commit comments

Comments
 (0)