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
Finally, to load records related via JSON:API relationships, use the `related` method. A nested resource URL is constructed like `categories/27/widgets`. (In the future we will look into using HATEOAS to let the server tell us the relationship URL).
By default, the name of the relationship on `parent` is assumed to be the same as the name of the other model: in this case, `widgets`. In cases where the names are not the same, you can explicitly pass the relationship name:
105
+
106
+
```javascript
107
+
constparent= {
108
+
type:'categories',
109
+
id:27,
110
+
};
111
+
112
+
constrelationship='purchased-widgets';
113
+
114
+
resource
115
+
.related({ parent, relationship })
116
+
.then(response=>console.log(response.data));
117
+
```
118
+
119
+
#### Options
120
+
121
+
All read methods take an optional `options` property, consisting of an object of additional options to pass. Each key/value pair in the object is translated into a query string parameter key/value pair:
122
+
123
+
```js
124
+
resource.all({
125
+
options: {
126
+
include:'comments',
127
+
},
128
+
});
129
+
130
+
// requests to widgets?include=comments
131
+
```
132
+
133
+
### Writing
134
+
135
+
#### create
136
+
137
+
Creates a new record. The object passed in should follow the JSON:API object format, but the `type` can be omitted:
138
+
139
+
```js
140
+
widgetResource.create({
141
+
attributes: {
142
+
'name':'My Widget',
143
+
'creation-date':'2018-10-07',
144
+
},
145
+
});
146
+
```
147
+
148
+
This isn't just limited to `attributes`; `relationships` can be passed in too.
149
+
150
+
#### update
151
+
152
+
Updates a record. Takes the `id` of the record and the `attributes` and/or `relationships` to update. No `type` argument is required, but if passed in it's ignored, so you can pass in a full record if you like.
153
+
154
+
```js
155
+
widgetResource.update({
156
+
id:'42',
157
+
attributes: {
158
+
name:'My Updated Widget',
159
+
},
160
+
});
161
+
```
162
+
163
+
This isn't just limited to `attributes`; `relationships` can be passed in too.
164
+
165
+
#### delete
166
+
167
+
Deletes the passed-in record. Only the `id` property is used, so you can pass either a full record or just the ID:
0 commit comments