Skip to content

Commit c2ed116

Browse files
committed
#3 Write a section about migrating from Adapters
1 parent 63fbf2a commit c2ed116

File tree

14 files changed

+181
-159
lines changed

14 files changed

+181
-159
lines changed

app/routes/application.js

Lines changed: 11 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -4,172 +4,25 @@ export default class ApplicationRoute extends Route {
44
model() {
55
return [
66
{
7-
id: 'generating-files',
7+
id: 'adapters',
88
subsections: [
99
{
10-
id: 'generating-component',
11-
classicFiles: ['classic.shell'],
12-
octaneFiles: ['octane.shell'],
10+
id: 'general',
1311
},
1412
{
15-
id: 'file-structure',
16-
classicFiles: ['classic.text'],
17-
octaneFiles: ['octane.text'],
18-
},
19-
],
20-
},
21-
{
22-
id: 'component-templates',
23-
subsections: [
24-
{
25-
id: 'angle-brackets',
26-
classicFiles: ['classic.hbs'],
27-
octaneFiles: ['octane.hbs'],
28-
},
29-
{
30-
id: 'inline-vs-block',
31-
classicFiles: ['classic.hbs'],
32-
octaneFiles: ['octane.hbs'],
33-
},
34-
{
35-
id: 'angle-brackets-nested',
36-
classicFiles: ['classic.hbs'],
37-
octaneFiles: ['octane.hbs'],
38-
},
39-
{
40-
id: 'template-named',
41-
classicFiles: ['classic.hbs'],
42-
octaneFiles: ['octane.hbs'],
43-
},
44-
{
45-
id: 'template-this',
46-
classicFiles: ['classic.hbs'],
47-
octaneFiles: ['octane.hbs'],
48-
},
49-
{
50-
id: 'template-arguments-named',
51-
classicFiles: ['classic.hbs'],
52-
octaneFiles: ['octane.hbs'],
53-
},
54-
{
55-
id: 'template-arguments-this',
56-
classicFiles: ['classic.hbs'],
57-
octaneFiles: ['octane.hbs'],
58-
},
59-
{
60-
id: 'tag-name',
61-
classicFiles: ['classic.hbs', 'classic.html'],
62-
octaneFiles: ['octane.hbs', 'octane.html'],
63-
},
64-
{
65-
id: 'element-id',
66-
classicFiles: ['classic.js', 'classic.hbs'],
67-
octaneFiles: ['octane.js', 'octane.hbs'],
68-
},
69-
],
70-
},
71-
{
72-
id: 'component-properties',
73-
subsections: [
74-
{
75-
id: 'js-boilerplate',
76-
classicFiles: ['classic.js'],
77-
octaneFiles: ['octane.js'],
78-
},
79-
{
80-
id: 'js-properties',
81-
classicFiles: ['classic.js'],
82-
octaneFiles: ['octane.js'],
83-
},
84-
{
85-
id: 'ddau',
86-
classicFiles: [
87-
'classic-parent.js',
88-
'classic-parent.hbs',
89-
'classic-child.js',
90-
'classic-child.hbs',
91-
],
92-
octaneFiles: [
93-
'octane-parent.js',
94-
'octane-parent.hbs',
95-
'octane-child.hbs',
96-
],
13+
id: 'host-and-namespace',
14+
classicFiles: ['old.js'],
15+
octaneFiles: ['new.js'],
9716
},
9817
{
99-
id: 'args',
100-
classicFiles: ['classic.js'],
101-
octaneFiles: ['octane.js'],
18+
id: 'path-for-type',
19+
classicFiles: ['old.js'],
20+
octaneFiles: ['new.js', 'utils.js'],
10221
},
10322
{
104-
id: 'get-and-set',
105-
classicFiles: ['classic.js'],
106-
octaneFiles: ['octane.js'],
107-
},
108-
{
109-
id: 'tracked-vs-cp',
110-
classicFiles: ['classic.js'],
111-
octaneFiles: ['octane.js'],
112-
},
113-
{
114-
id: 'computed-decorator',
115-
classicFiles: ['classic.js'],
116-
octaneFiles: ['octane.js'],
117-
},
118-
],
119-
},
120-
{
121-
id: 'actions',
122-
subsections: [
123-
{
124-
id: 'actions',
125-
classicFiles: ['classic.js', 'classic.hbs'],
126-
octaneFiles: ['octane.js', 'octane.hbs'],
127-
},
128-
{
129-
id: 'template-arguments-default',
130-
classicFiles: ['classic.hbs', 'classic.js'],
131-
octaneFiles: ['octane.hbs', 'octane.js'],
132-
},
133-
{
134-
id: 'mixins',
135-
classicFiles: ['classic.js'],
136-
octaneDescriptionKey: 'actions.mixins.octaneDescription',
137-
},
138-
],
139-
},
140-
{
141-
id: 'component-lifecycle',
142-
subsections: [
143-
{
144-
id: 'constructors',
145-
classicFiles: ['classic.js'],
146-
octaneFiles: ['octane.js'],
147-
},
148-
{
149-
id: 'will-destroy',
150-
classicFiles: ['classic.js'],
151-
octaneFiles: ['octane.js'],
152-
},
153-
{
154-
id: 'render-modifiers',
155-
classicDescriptionKey:
156-
'component-lifecycle.render-modifiers.classicDescription',
157-
octaneFiles: ['octane.shell'],
158-
},
159-
{
160-
id: 'did-insert',
161-
classicFiles: ['classic.hbs', 'classic.js'],
162-
octaneFiles: ['octane.hbs', 'octane.js'],
163-
},
164-
],
165-
},
166-
{
167-
id: 'routes',
168-
subsections: [
169-
{
170-
id: 'model-access',
171-
classicFiles: ['classic.hbs'],
172-
octaneFiles: ['octane.hbs'],
23+
id: 'cache-lifetime',
24+
classicFiles: ['old.js'],
25+
octaneFiles: ['new.js'],
17326
},
17427
],
17528
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { LifetimesService } from '@ember-data/request-utils';
2+
import DataStore from '@ember-data/store';
3+
4+
export default class Store extends DataStore {
5+
constructor(args) {
6+
super(args);
7+
this.lifetimes = new LifetimesService(this, {
8+
apiCacheSoftExpires: 30_000,
9+
apiCacheHardExpires: 60_000
10+
});
11+
}
12+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import JSONAPIAdapter from '@ember-data/adapter/json-api';
2+
3+
export default class ApplicationAdapter extends JSONAPIAdapter {
4+
shouldBackgroundReloadAll(store, snapshotArray) {
5+
let { downlink, effectiveType } = navigator.connection;
6+
7+
return downlink > 0 && effectiveType === '4g';
8+
}
9+
10+
shouldBackgroundReloadRecord(store, snapshot) {
11+
let { downlink, effectiveType } = navigator.connection;
12+
13+
return downlink > 0 && effectiveType === '4g';
14+
}
15+
16+
shouldReloadRecord(store, ticketSnapshot) {
17+
let lastAccessedAt = ticketSnapshot.attr('lastAccessedAt');
18+
let timeDiff = moment().diff(lastAccessedAt, 'minutes');
19+
20+
if (timeDiff > 20) {
21+
return true;
22+
} else {
23+
return false;
24+
}
25+
}
26+
27+
shouldReloadAll(store, snapshotArray) {
28+
let snapshots = snapshotArray.snapshots();
29+
30+
return snapshots.any((ticketSnapshot) => {
31+
let lastAccessedAt = ticketSnapshot.attr('lastAccessedAt');
32+
let timeDiff = moment().diff(lastAccessedAt, 'minutes');
33+
34+
if (timeDiff > 20) {
35+
return true;
36+
} else {
37+
return false;
38+
}
39+
});
40+
}
41+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { setBuildURLConfig } from '@ember-data/request-utils';
2+
import config from 'my-app/config/environment';
3+
4+
setBuildURLConfig({
5+
host: config.api.host,
6+
namespace: config.api.namespace,
7+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import JSONAPIAdapter from '@ember-data/adapter/json-api';
2+
import config from 'my-app/config/environment';
3+
4+
export default class ApplicationAdapter extends JSONAPIAdapter {
5+
host = config.api.host;
6+
namespace = config.api.namespace;
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { findRecord } from '@ember-data/json-api/request';
2+
// import { findRecord } from '@ember-data/rest/request';
3+
// import { findRecord } from '@ember-data/active-record/request';
4+
5+
const dynamicPathFor = (identifierType) => {
6+
const collectionName = camelize(pluralize(identifierType))
7+
return `collections/${collectionName}/records`;
8+
};
9+
10+
const options = findRecord('post', '1', {
11+
resourcePath: dynamicPathFor('post'),
12+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default class ApplicationAdapter extends JSONAPIAdapter {
2+
pathForType(type) {
3+
const collectionName = pluralize(camelize(type));
4+
return `collections/${collectionName}/records`;
5+
}
6+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { camelize } from '@ember/string';
2+
import { pluralize } from 'ember-inflector';
3+
import { recordIdentifierFor } from '@ember-data/store';
4+
5+
import {
6+
findRecord as restFindRecord,
7+
query as restQuery,
8+
createRecord as restCreateRecord,
9+
updateRecord as restUpdateRecord,
10+
deleteRecord as restDeleteRecord,
11+
} from '@ember-data/rest/request';
12+
13+
const _customResourcePath = (identifierType) => {
14+
return `collections/${camelize(pluralize(identifierType))}/records`;
15+
};
16+
17+
const findRecord = (identifierType, id, options) => {
18+
options = {
19+
...options,
20+
resourcePath: _customResourcePath(identifierType),
21+
};
22+
return restFindRecord(identifierType, id, options);
23+
};
24+
25+
const query = (identifierType, query, options) => {
26+
options = {
27+
...options,
28+
resourcePath: _customResourcePath(identifierType),
29+
};
30+
return restQuery(identifierType, query, options);
31+
};
32+
33+
const createRecord = (record, options) => {
34+
const identifier = recordIdentifierFor(record);
35+
options = {
36+
...options,
37+
resourcePath: _customResourcePath(identifier.type),
38+
};
39+
return restCreateRecord(record, options);
40+
};
41+
42+
const updateRecord = (record, options) => {
43+
const identifier = recordIdentifierFor(record);
44+
options = {
45+
...options,
46+
resourcePath: _customResourcePath(identifier.type),
47+
};
48+
return restUpdateRecord(record, options);
49+
};
50+
51+
const deleteRecord = (record, options) => {
52+
const identifier = recordIdentifierFor(record);
53+
options = {
54+
...options,
55+
resourcePath: _customResourcePath(identifier.type),
56+
};
57+
return restDeleteRecord(record, options);
58+
};
59+
60+
export { findRecord, query, createRecord, updateRecord, deleteRecord };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
title: Cache lifetime
2+
description: >
3+
In the past, cache lifetimes for single resources were controlled by either supplying the '<code>'reload'</code>' and '<code>'backgroundReload'</code>' options or by the Adapter's hooks for '<code>'shouldReloadRecord'</code>', '<code>'shouldReloadAll'</code>', '<code>'shouldBackgroundReloadRecord'</code>' and '<code>'shouldBackgroundReloadAll'</code>'.
4+
This behavior will now be controlled by the combination of either supplying '<code>'cacheOptions'</code>' on the associated '<code>'RequestInfo'</code>' or by supplying a '<code>'lifetimes'</code>' service to the '<code>'Store'</code>'.

translations/adapters/en-us.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: Adapters

0 commit comments

Comments
 (0)