Skip to content

Commit

Permalink
Fix review comments from Chris
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltazore committed Oct 16, 2023
1 parent 993a6af commit efcfecd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions snippets/fetching-data/find-all/new.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { query } from '@ember-data/json-api/request';

const result = await store.request(query('user'));
const users = result.content.data;
await this.store.request(query('user'));
const users = this.store.peekAll('user')
3 changes: 2 additions & 1 deletion snippets/fetching-data/find-record/new.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { findRecord } from '@ember-data/json-api/request';

const { content: { data: user } } = await this.store.request(findRecord('user', '1'));
const result = await this.store.request(findRecord('user', '1'));
const user = result.content.data
38 changes: 33 additions & 5 deletions snippets/fetching-data/find-record/own-builder.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
// Bring your own builder
import { findRecord as edFindRecord } from '@ember-data/json-api/request';
import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils'
import { pluralize } from 'ember-inflector';

async function findRecord(typeOrIdentifier, id, options) {
const result = await edFindRecord(typeOrIdentifier, id, options);
async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) {
const identifier = typeof typeOrIdentifier === 'string' ? { type: typeOrIdentifier, id } : typeOrIdentifier;
const options = ((typeof typeOrIdentifier === 'string' ? maybeOptions : idOrOptions) || {});

const urlOptions = {
op: 'findRecord',
identifier,
resourcePath: pluralize(identifier.type),
};

const url = buildBaseURL(urlOptions);
const headers = new Headers();
headers.append('Accept', 'application/vnd.api+json');
headers.append('Content-Type', 'application/vnd.api+json');

return {
url: options.include?.length
? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}`
: url,
method: 'GET',
headers,
op: 'findRecord',
records: [identifier],
};

return result.content.data;
}

export default {
findRecord
};

// Somewhere in app
const user = await this.store.findRecord('user', '1');
const fetchOptions = findRecord('user', '1', { include: 'friends' });
const result = await this.store.request(fetchOptions)
const user = result.content.data
// or using identifier for findRecord builder
const fetchOptions = findRecord({ type: 'user', id: '1' }, { include: 'friends' });
const result = await this.store.request(fetchOptions)
const user = result.content.data


0 comments on commit efcfecd

Please sign in to comment.