You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `fetch` method works almost as same as `get` method, but it will take URL query parameters as the 2nd argument. Please [refer here](../guide/advanced-usage.html#fetch) for more details.
Copy file name to clipboardExpand all lines: docs/guide/configurations.md
+40-4Lines changed: 40 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ import VuexORMAxios from '@vuex-orm/plugin-axios'
15
15
16
16
VuexORM.use(VuexORMAxios, {
17
17
axios,
18
-
headers: {'X-Requested-With':'XMLHttpRequest'},
18
+
headers: {'X-Requested-With':'XMLHttpRequest'},
19
19
baseURL:'https://example.com/api/'
20
20
})
21
21
```
@@ -36,7 +36,7 @@ class User extends Model {
36
36
}
37
37
38
38
static apiConfig = {
39
-
headers: {'X-Requested-With':'XMLHttpRequest'},
39
+
headers: {'X-Requested-With':'XMLHttpRequest'},
40
40
baseURL:'https://example.com/api/'
41
41
}
42
42
}
@@ -46,7 +46,7 @@ Finally, you can pass configuration when making the api call.
46
46
47
47
```js
48
48
User.api().get('/api/users', {
49
-
headers: {'X-Requested-With':'XMLHttpRequest'},
49
+
headers: {'X-Requested-With':'XMLHttpRequest'},
50
50
baseURL:'https://example.com/api/'
51
51
})
52
52
```
@@ -59,7 +59,9 @@ All Axios configurations are available. For those, please refer to [the Axios do
59
59
60
60
### dataKey
61
61
62
-
-**`dataKey: string | null`**
62
+
-**`dataKey?: string | null`**
63
+
64
+
This option will define which key to look for when persisting response data. Let's say your response from the server looks like below.
63
65
64
66
```js
65
67
{
@@ -82,3 +84,37 @@ All Axios configurations are available. For those, please refer to [the Axios do
82
84
```
83
85
84
86
With the above config, the data inside `data` key will be inserted to the store.
87
+
88
+
### save
89
+
90
+
-**`save: boolean = true`**
91
+
92
+
This option will determine whether to store the response data to the store or not. If you set this value to `false`, the response data will not be persisted to the store. In that case, the `entities` property at the Response object will become null.
93
+
94
+
```js
95
+
User.api().get('/api/users', {
96
+
save:false
97
+
})
98
+
```
99
+
100
+
### delete
101
+
102
+
-**`delete?: string | number | (model: Model) => boolean`**
103
+
104
+
When this option is defined, the matching record will be deleted from the store after the api call. Usually, you need to set this when calling api to delete a record. When this option is set, the response data will not be persisted even if the `save` option is set to true.
105
+
106
+
```js
107
+
User.api().delete('/api/users', {
108
+
delete:1
109
+
})
110
+
```
111
+
112
+
Well, you may wonder having to manually specify what record to be deleted is a bit redundant. However, without this option, Vuex ORM Axios wouldn't know what records should be deleted because it can't rely on the response data.
113
+
114
+
We're open to any suggestions and recommendations to improve the "delete" functionality. Please feel free to open an issue on GitHub!
The Custom Actions lets you define your own predefined api methods. You can define any number of custom actions through your Model configurations through `actions` option.
4
+
5
+
```js
6
+
classUserextendsModel {
7
+
static entity ='users'
8
+
9
+
staticfields () {
10
+
return {
11
+
id:this.attr(null),
12
+
name:this.attr('')
13
+
}
14
+
}
15
+
16
+
static apiConfig = {
17
+
actions: {
18
+
fetch: {
19
+
method:'get',
20
+
url:'/api/users'
21
+
}
22
+
}
23
+
}
24
+
}
25
+
```
26
+
27
+
You can see that in the above example, we have defined `fetch` action with the option that defines the `method` and `url`.
28
+
29
+
Now you may call this action as if it was a predefined method.
30
+
31
+
```js
32
+
User.api().fetch()
33
+
```
34
+
35
+
The above method will perform api call to `/api/users` with `GET` method. Now the value for the action (in the example, which is the object that defines `method` and `url`) is the request configuration. Now you see that the above example is equivalent to calling:
36
+
37
+
```js
38
+
User.api().request({
39
+
method:'get',
40
+
url:'/api/users'
41
+
})
42
+
```
43
+
44
+
Actions can also be defined as a function. In this case, just call the desired method with in action. With this approach, you can configure the convenience argument to the action, and gives you more powerful control.
45
+
46
+
Remember that inside the function, `this` is bind to the Request object, not the Model where the actions are defined.
47
+
48
+
```js
49
+
classUserextendsModel {
50
+
static apiConfig = {
51
+
actions: {
52
+
fetchById (id) {
53
+
returnthis.get(`/api/users/${id}`)
54
+
}
55
+
}
56
+
}
57
+
}
58
+
```
59
+
60
+
And you can call that action like so.
61
+
62
+
```js
63
+
User.api().fetchById(1)
64
+
```
65
+
66
+
## When to Use Custom Actions?
67
+
68
+
While the custom actions are convenient and easy to set up, you can always define methods to the Model directly to get pretty much the same result.
69
+
70
+
```js
71
+
classUserextendsModel {
72
+
staticfetchById (id) {
73
+
returnthis.api().get(`/api/users/${id}`)
74
+
}
75
+
}
76
+
```
77
+
78
+
Well of crouse in this case, you must call that method from Model and not from `api()` method.
79
+
80
+
```js
81
+
User.fetchById(1)
82
+
```
83
+
84
+
To be honest, this is a much better way to define custom methods in terms of simplicity and also better with type safety when using TypeScript.
85
+
86
+
The benefits of defining custom actions inside the configuration are that you can put those methods under Request object, so it becomes more consistent when calling it from the Model. Also, it could be easier to share custom actions between different Models.
87
+
88
+
It's up to you how to define custom actions. Though if you have any ideas or feedback, we're more than happy to hear it from you!
Copy file name to clipboardExpand all lines: docs/guide/setup.md
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,6 @@
1
1
# Setup
2
2
3
-
This page is a quick start guide to begin using Vuex ORM. It assumes you have a basic understanding of Vuex ORM. If you are not familiar with Vuex ORM, please check out the [Vuex ORM Documentation](https://vuex-orm.github.io/vuex-orm/).
4
-
5
-
At first, Vuex ORM Axios requires manually passing Axios instance during the installation process. Please make sure you have axios installed to your app.
3
+
At first, Vuex ORM Axios requires manually passing Axios instance during the setup process. Please make sure you have axios installed to your app.
6
4
7
5
To install Vuex ORM Axios to Vuex ORM, pass Vuex ORM Axios to the `VuexORM.use` method. Here, you should pass your axios instance as an option.
Copy file name to clipboardExpand all lines: docs/guide/usage.md
+13-1Lines changed: 13 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Basic Usage
1
+
# Usage
2
2
3
3
After setting up Vuex ORM Axios, you may use `Model.api` method to perform api call.
4
4
@@ -61,6 +61,18 @@ User.api().get('users', {
61
61
62
62
There're additional configuration specific to Vuex ORM Axios as well. Please check out [Configurations page](configurations) for more.
63
63
64
+
### Note on Delete Method
65
+
66
+
Even when you call `delete` method, it will not delete records from the store. It just means that it will perform HTTP DELETE request. If you want to delete the record after calling the API, you must define `delete` option.
67
+
68
+
```js
69
+
User.api().delete('/api/users/1', {
70
+
delete:1
71
+
})
72
+
```
73
+
74
+
The above example will delete the user record with an id of 1. Please check out [Configurations page](configurations) for more.
75
+
64
76
## Response
65
77
66
78
The response object is a bit different from Axios response and contains 2 properties. One is `response`, which is the pure Axios response object, and the second one is the `entities`, which holds Vuex ORM persistent result.
0 commit comments