Skip to content

Commit 22fbf24

Browse files
committed
Add update section
1 parent 298111f commit 22fbf24

File tree

9 files changed

+66
-0
lines changed

9 files changed

+66
-0
lines changed

app/routes/application.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ export default class ApplicationRoute extends Route {
2828
},
2929
],
3030
},
31+
{
32+
id: 'updating-data',
33+
subsections: [
34+
{
35+
id: 'create-record',
36+
classicFiles: ['old.js'],
37+
octaneFiles: ['new.js'],
38+
},
39+
{
40+
id: 'save-record',
41+
classicFiles: ['old.js'],
42+
octaneFiles: ['new.js'],
43+
},
44+
],
45+
},
3146
{
3247
id: 'adapters',
3348
subsections: [
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { recordIdentifierFor } from '@ember-data/store';
2+
import { createRecord, serializeResources } from '@ember-data/json-api/request';
3+
4+
const record = store.createRecord('user', {});
5+
const request = createRecord(record);
6+
request.body = JSON.stringify(
7+
serializeResources(
8+
store.cache,
9+
recordIdentifierFor(record)
10+
)
11+
);
12+
13+
await store.request(request);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const record = this.store.createRecord('user', { name: "Chris" });
2+
await record.save();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { recordIdentifierFor } from '@ember-data/store';
2+
import { updateRecord, serializePatch } from '@ember-data/json-api/request';
3+
4+
user.name = 'Chris';
5+
6+
const request = updateRecord(user);
7+
request.body = JSON.stringify(
8+
serializePatch(
9+
store.cache,
10+
recordIdentifierFor(user)
11+
)
12+
);
13+
14+
await store.request(request);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const user = this.store.peekRecord('user', '1');
2+
3+
user.name = 'Chris';
4+
await user.save();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
title: createRecord
2+
description: To create a new record using Ember Data you should use <code>createRecord</code> request and attach <code>"body"</code> to it. In case of JSON:API backend - you can user <code>serializeResources</code> request utility.

translations/updating-data/en-us.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: Creating/Updating Data
2+
description: >
3+
For requests that are expected to send a "body" to the API applications may choose to either serialize the body at the point of the request or to implement a Handler for the RequestManager to do so.
4+
5+
EmberData does not provide a default handler which serializes because this is a unique concern for every app. However, EmberData does provide utilities on both the Cache and for some of the builders to make this easy.
6+
7+
For JSON:API we show the "at point of request" approach using the utils provided by the @ember-data/json-api package here.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: General concepts / philosophy
2+
description: >
3+
For requests that are expected to send a <code>"body"</code> to the API applications may choose to either serialize the body at the point of the request or to implement a <code>Handler</code> for the <code>RequestManager</code> to do so.
4+
5+
Ember Data does not provide a default handler which serializes because this is a unique concern for every app. However, Ember Data does provide utilities on both the <code>Cache</code> and for some of the <code>builders</code> to make this easy.
6+
7+
For <code>JSON:API</code> we show the "at point of request" approach using the utils provided by the <code>@ember-data/json-api</code> package here.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
title: saveRecord / updateRecord
2+
description: To update record, you should send <code>updateRecord</code> request to your backend and attach <code>"body"</code> to it. Based on your backend server needs, you can use <code>serializeResources</code> or <code>serializePatch</code> request utilities.

0 commit comments

Comments
 (0)