Skip to content

Commit 56ea471

Browse files
committed
Add findRecord section
1 parent f31e5e4 commit 56ea471

File tree

7 files changed

+49
-0
lines changed

7 files changed

+49
-0
lines changed

app/routes/application.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import Route from '@ember/routing/route';
33
export default class ApplicationRoute extends Route {
44
model() {
55
return [
6+
{
7+
id: 'fetching-data',
8+
subsections: [
9+
{
10+
id: 'find-record',
11+
classicFiles: ['old.js'],
12+
octaneFiles: ['new.js', 'replicate-store.js', 'own-builder.js'],
13+
},
14+
],
15+
},
616
{
717
id: 'adapters',
818
subsections: [
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { findRecord } from '@ember-data/json-api/request';
2+
3+
const { content: { data: user } } = await this.store.request(findRecord('user', '1'));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const user = await this.store.findRecord('user', '1');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Bring your own builder
2+
import { findRecord as edFindRecord } from '@ember-data/json-api/request';
3+
4+
async function findRecord(typeOrIdentifier, id, options) {
5+
const result = await edFindRecord(typeOrIdentifier, id, options);
6+
7+
return result.content.data;
8+
}
9+
10+
export default {
11+
findRecord
12+
};
13+
14+
// Somewhere in app
15+
const user = await this.store.findRecord('user', '1');
16+
17+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Replicate old on store methods
2+
import { findRecord } from '@ember-data/json-api/request';
3+
import DataStore from '@ember-data/store'
4+
5+
export default class Store extends DataStore {
6+
async findRecord(typeOrIdentifier, id, options) {
7+
const result = await this.request(findRecord(typeOrIdentifier, id, options));
8+
// but you might want to return whole result object, as it has meta, errors, links, etc.
9+
return result.content.data;
10+
}
11+
}
12+
13+
// Somewhere in app
14+
const user = await this.store.findRecord('user', '1')

translations/fetching-data/en-us.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: Fetching Data
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
title: Find Record
2+
description: |
3+
Examples here are shown for apps that use JSON:API. Apps using other paradigms should use the builders for REST or ActiveRecord if applicable, or author their own (or a new community lib!) if not.

0 commit comments

Comments
 (0)