-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|