Skip to content

Commit

Permalink
Chapter 11 EmberData with RequestManager and Builders
Browse files Browse the repository at this point in the history
  • Loading branch information
ember-tomster committed Jul 6, 2024
1 parent c4f7415 commit 1c4cb49
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 33 deletions.
9 changes: 0 additions & 9 deletions app/adapters/application.js

This file was deleted.

5 changes: 5 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import Application from '@ember/application';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from 'super-rentals/config/environment';
import { setBuildURLConfig } from '@ember-data/request-utils';

setBuildURLConfig({
namespace: 'api',
});

export default class App extends Application {
modulePrefix = config.modulePrefix;
Expand Down
4 changes: 3 additions & 1 deletion app/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Route from '@ember/routing/route';
import { service } from '@ember/service';
import { query } from '@ember-data/json-api/request';

export default class IndexRoute extends Route {
@service store;

async model() {
return this.store.findAll('rental');
const { content } = await this.store.request(query('rental'));
return content.data;
}
}
6 changes: 5 additions & 1 deletion app/routes/rental.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Route from '@ember/routing/route';
import { service } from '@ember/service';
import { findRecord } from '@ember-data/json-api/request';

export default class RentalRoute extends Route {
@service store;

async model(params) {
return this.store.findRecord('rental', params.rental_id);
const { content } = await this.store.request(
findRecord('rental', params.rental_id),
);
return content.data;
}
}
3 changes: 0 additions & 3 deletions app/serializers/application.js

This file was deleted.

11 changes: 11 additions & 0 deletions app/services/request-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import BaseRequestManager from '@ember-data/request';
import Fetch from '@ember-data/request/fetch';
import { JsonSuffixHandler } from 'super-rentals/utils/handlers';

export default class RequestManager extends BaseRequestManager {
constructor(args) {
super(args);

this.use([JsonSuffixHandler, Fetch]);
}
}
8 changes: 7 additions & 1 deletion app/services/store.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export { default } from 'ember-data/store';
// eslint-disable-next-line ember/use-ember-data-rfc-395-imports
import BaseStore from 'ember-data/store';
import { service } from '@ember/service';

export default class Store extends BaseStore {
@service requestManager;
}
10 changes: 10 additions & 0 deletions app/utils/handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const JsonSuffixHandler = {
request(context, next) {
const { request } = context;
const updatedRequest = Object.assign({}, request, {
url: request.url + '.json',
});

return next(updatedRequest);
},
};
6 changes: 3 additions & 3 deletions public/api/rentals.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"data": [
{
"type": "rentals",
"type": "rental",
"id": "grand-old-mansion",
"attributes": {
"title": "Grand Old Mansion",
Expand All @@ -18,7 +18,7 @@
}
},
{
"type": "rentals",
"type": "rental",
"id": "urban-living",
"attributes": {
"title": "Urban Living",
Expand All @@ -35,7 +35,7 @@
}
},
{
"type": "rentals",
"type": "rental",
"id": "downtown-charm",
"attributes": {
"title": "Downtown Charm",
Expand Down
2 changes: 1 addition & 1 deletion public/api/rentals/downtown-charm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": {
"type": "rentals",
"type": "rental",
"id": "downtown-charm",
"attributes": {
"title": "Downtown Charm",
Expand Down
2 changes: 1 addition & 1 deletion public/api/rentals/grand-old-mansion.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": {
"type": "rentals",
"type": "rental",
"id": "grand-old-mansion",
"attributes": {
"title": "Grand Old Mansion",
Expand Down
2 changes: 1 addition & 1 deletion public/api/rentals/urban-living.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": {
"type": "rentals",
"type": "rental",
"id": "urban-living",
"attributes": {
"title": "Urban Living",
Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1079,19 +1079,19 @@
minimist "^1.2.0"

"@csstools/css-parser-algorithms@^2.3.1":
version "2.7.0"
resolved "https://registry.yarnpkg.com/@csstools/css-parser-algorithms/-/css-parser-algorithms-2.7.0.tgz#ee41f252438b97045db2528c1a999e95f15610d8"
integrity sha512-qvBMcOU/uWFCH/VO0MYe0AMs0BGMWAt6FTryMbFIKYtZtVnqTZtT8ktv5o718llkaGZWomJezJZjq3vJDHeJNQ==
version "2.7.1"
resolved "https://registry.yarnpkg.com/@csstools/css-parser-algorithms/-/css-parser-algorithms-2.7.1.tgz#6d93a8f7d8aeb7cd9ed0868f946e46f021b6aa70"
integrity sha512-2SJS42gxmACHgikc1WGesXLIT8d/q2l0UFM7TaEeIzdFCE/FPMtTiizcPGGJtlPo2xuQzY09OhrLTzRxqJqwGw==

"@csstools/css-tokenizer@^2.2.0":
version "2.4.1"
resolved "https://registry.yarnpkg.com/@csstools/css-tokenizer/-/css-tokenizer-2.4.1.tgz#1d8b2e200197cf5f35ceb07ca2dade31f3a00ae8"
integrity sha512-eQ9DIktFJBhGjioABJRtUucoWR2mwllurfnM8LuNGAqX3ViZXaUchqk+1s7jjtkFiT9ySdACsFEA3etErkALUg==

"@csstools/media-query-list-parser@^2.1.4":
version "2.1.12"
resolved "https://registry.yarnpkg.com/@csstools/media-query-list-parser/-/media-query-list-parser-2.1.12.tgz#ec7485667efbaad420b3414dc1dc19dc7a49f626"
integrity sha512-t1/CdyVJzOQUiGUcIBXRzTAkWTFPxiPnoKwowKW2z9Uj78c2bBWI/X94BeVfUwVq1xtCjD7dnO8kS6WONgp8Jw==
version "2.1.13"
resolved "https://registry.yarnpkg.com/@csstools/media-query-list-parser/-/media-query-list-parser-2.1.13.tgz#f00be93f6bede07c14ddf51a168ad2748e4fe9e5"
integrity sha512-XaHr+16KRU9Gf8XLi3q8kDlI18d5vzKSKCY510Vrtc9iNR0NJzbY9hhTmwhzYZj/ZwGL4VmB3TA9hJW0Um2qFA==

"@csstools/selector-specificity@^3.0.0":
version "3.1.1"
Expand Down Expand Up @@ -4105,9 +4105,9 @@ [email protected]:
integrity sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==

electron-to-chromium@^1.4.796:
version "1.4.817"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.817.tgz#da9808e35212408cf4052b51679786fcb54fec39"
integrity sha512-3znu+lZMIbTe8ZOs360OMJvVroVF2NpNI8T5jfLnDetVvj0uNmIucZzQVYMSJfsu9f47Ssox1Gt46PR+R+1JUg==
version "1.4.818"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.818.tgz#7762c8bfd15a07c3833b7f5deed990e9e5a4c24f"
integrity sha512-eGvIk2V0dGImV9gWLq8fDfTTsCAeMDwZqEPMr+jMInxZdnp9Us8UpovYpRCf9NQ7VOFgrN2doNSgvISbsbNpxA==

ember-auto-import@^2.6.0, ember-auto-import@^2.6.3, ember-auto-import@^2.7.3:
version "2.7.4"
Expand Down Expand Up @@ -7306,9 +7306,9 @@ lower-case@^2.0.2:
tslib "^2.0.3"

lru-cache@^10.2.0:
version "10.3.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.3.0.tgz#4a4aaf10c84658ab70f79a85a9a3f1e1fb11196b"
integrity sha512-CQl19J/g+Hbjbv4Y3mFNNXFEL/5t/KCg8POCuUqd4rMKjGG+j1ybER83hxV58zL+dFI1PTkt3GNFSHRt+d8qEQ==
version "10.3.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.3.1.tgz#a37050586f84ccfdb570148a253bf1632a29ef44"
integrity sha512-9/8QXrtbGeMB6LxwQd4x1tIMnsmUxMvIH/qWGsccz6bt9Uln3S+sgAaqfQNhbGA8ufzs2fHuP/yqapGgP9Hh2g==

lru-cache@^5.1.1:
version "5.1.1"
Expand Down

0 comments on commit 1c4cb49

Please sign in to comment.