Skip to content

Commit

Permalink
Add findRecord section
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltazore committed Oct 16, 2023
1 parent 2ed06b6 commit 13bec91
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import Route from '@ember/routing/route';
export default class ApplicationRoute extends Route {
model() {
return [
{
id: 'fetching-data',
subsections: [
{
id: 'find-record',
classicFiles: ['old.js'],
octaneFiles: ['new.js', 'replicate-store.js', 'own-builder.js'],
},
],
},
{
id: 'adapters',
subsections: [
Expand Down
3 changes: 3 additions & 0 deletions snippets/fetching-data/find-record/new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { findRecord } from '@ember-data/json-api/request';

const { content: { data: user } } = await this.store.request(findRecord('user', '1'));
1 change: 1 addition & 0 deletions snippets/fetching-data/find-record/old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const user = await this.store.findRecord('user', '1');
17 changes: 17 additions & 0 deletions snippets/fetching-data/find-record/own-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Bring your own builder
import { findRecord as edFindRecord } from '@ember-data/json-api/request';

async function findRecord(typeOrIdentifier, id, options) {
const result = await edFindRecord(typeOrIdentifier, id, options);

return result.content.data;
}

export default {
findRecord
};

// Somewhere in app
const user = await this.store.findRecord('user', '1');


14 changes: 14 additions & 0 deletions snippets/fetching-data/find-record/replicate-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Replicate old on store methods
import { findRecord } from '@ember-data/json-api/request';
import DataStore from '@ember-data/store'

export default class Store extends DataStore {
async findRecord(typeOrIdentifier, id, options) {
const result = await this.request(findRecord(typeOrIdentifier, id, options));
// but you might want to return whole result object, as it has meta, errors, links, etc.
return result.content.data;
}
}

// Somewhere in app
const user = await this.store.findRecord('user', '1')
1 change: 1 addition & 0 deletions translations/fetching-data/en-us.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Fetching Data
3 changes: 3 additions & 0 deletions translations/fetching-data/find-record/en-us.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: Find Record
description: |
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 comments on commit 13bec91

Please sign in to comment.