Skip to content

Commit cf97e73

Browse files
maciekkusf
authored andcommitted
ability to customize method, action and loader for mapWaitingActions; support for array as a second argument to mapWaitingActions (#55)
* support for { method: , action: , loader: } in mapWaitingActions, issue-50 * fix for fix for issue-50
1 parent 7f140e7 commit cf97e73

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,36 @@ import { mapWaitingActions, mapWaitingGetters } from 'vue-wait'
420420
}),
421421
}
422422
// ...
423+
```
424+
425+
You can also map **action** to custom method and customize loader name like in example below:
426+
427+
```js
428+
import { mapWaitingActions, mapWaitingGetters } from 'vue-wait'
429+
430+
// ...
431+
methods: {
432+
...mapWaitingActions('users', {
433+
getUsers: { action: 'getUsers', loader: 'loading users' },
434+
createUser: { action: 'createUser', loader: 'creating user'},
435+
createSuperUser: { action: 'createUser', loader: 'creating super user' },
436+
}),
437+
},
438+
// ...
439+
```
440+
441+
There is also possibility to use array as a second argument to mapWaitingActions:
442+
```js
443+
// ...
444+
methods: {
445+
...mapWaitingActions('users', [
446+
'getUsers',
447+
{ method: 'createUser', action: 'createUser', loader: 'creating user'},
448+
{ method: 'createSuperUser', action: 'createUser', loader: 'creating super user' },
449+
]),
450+
},
451+
// ...
452+
423453

424454
```
425455

dist/vue-wait.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/helpers.js

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,47 @@ export function mapWaitingActions(vuexModuleName, actions) {
44
actions = vuexModuleName;
55
vuexModuleName = null;
66
}
7-
Object.keys(actions).forEach(action => {
8-
const waiter = actions[action];
9-
mappings[action] = async function(...args) {
10-
try {
11-
this.__$waitInstance.start(waiter);
12-
return await this.$store.dispatch(
13-
vuexModuleName ? `${vuexModuleName}/${action}` : action,
14-
...args
15-
);
16-
} finally {
17-
this.__$waitInstance.end(waiter);
7+
const isActionsArray = Array.isArray(actions);
8+
9+
for (let [key, entry] of Object.entries(actions)) {
10+
let method, action, waiter;
11+
if (entry === Object(entry)) {
12+
if (isActionsArray) {
13+
method = entry.action;
14+
if (entry.method !== undefined) {
15+
method = entry.method;
16+
}
17+
} else {
18+
method = key;
1819
}
19-
};
20-
});
20+
action = entry.action;
21+
waiter = entry.loader;
22+
} else {
23+
if (isActionsArray) {
24+
method = action = entry;
25+
waiter = entry;
26+
} else {
27+
method = action = key;
28+
waiter = entry;
29+
}
30+
}
31+
if (!waiter) {
32+
waiter = action;
33+
}
34+
if (action) {
35+
mappings[method] = async function(...args) {
36+
try {
37+
this.__$waitInstance.start(waiter);
38+
return await this.$store.dispatch(
39+
vuexModuleName ? `${vuexModuleName}/${action}` : action,
40+
...args
41+
);
42+
} finally {
43+
this.__$waitInstance.end(waiter);
44+
}
45+
};
46+
}
47+
}
2148
return mappings;
2249
}
2350

0 commit comments

Comments
 (0)