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
You can create records by calling the `createRecord()` method on the datastore:
222
227
- The first argument is the type of object you want to create.
@@ -226,39 +231,53 @@ You can create records by calling the `createRecord()` method on the datastore:
226
231
this.datastore.createRecord(Post, {
227
232
title: 'My post',
228
233
content: 'My content'
229
-
}).subscribe(
234
+
});
235
+
```
236
+
237
+
#### Persisting Records
238
+
239
+
Records are persisted on a per-instance basis. Call `save()` on any instance of `JsonApiModel` and it will make a network request.
240
+
241
+
```typescript
242
+
let post =this.datastore.createRecord(Post, {
243
+
title: 'My post',
244
+
content: 'My content'
245
+
});
246
+
247
+
post.save();
248
+
```
249
+
250
+
The `save()` method will return an `Observer` that you can subscribe:
251
+
252
+
```typescript
253
+
post.save().subscribe(
230
254
(post:Post) =>console.log(post)
231
255
);
232
256
```
233
257
258
+
#### Relationships
259
+
234
260
If the object you want to create has a **one-to-many** relationship, you can do this:
235
261
236
262
```typescript
237
-
this.datastore.createRecord(Comment, {
263
+
let comment =this.datastore.createRecord(Comment, {
238
264
title: 'My comment',
239
265
post: post
240
-
}).subscribe(
241
-
(comment:Comment) =>console.log(comment)
242
-
);
266
+
});
243
267
```
244
268
245
269
where `post` is an object of type `Post`, previously retrieved from the API.
246
270
247
-
If you want to include a relationship when creating a record to have it parsed in the response, you can pass the `params` object:
271
+
If you want to include a relationship when creating a record to have it parsed in the response, you can pass the `params` object to the `save()` method:
248
272
249
273
```typescript
250
-
this.datastore.createRecord(Comment, {
251
-
title: 'My comment',
252
-
post: post
253
-
}, {
274
+
comment.save({
254
275
include: 'user'
255
276
}).subscribe(
256
277
(comment:Comment) =>console.log(comment)
257
278
);
258
279
```
259
280
260
-
261
-
262
281
### Custom Headers
263
282
264
283
By default, the library adds these headers, according to the [JSON API MIME Types](http://jsonapi.org/#mime-types):
0 commit comments