Skip to content

Commit dd2d7fb

Browse files
authored
Merge pull request #1319 from bartocc/ember-data-4-x-deprecation-guides
📝 Add all 4.x ember-data deprecation pages
2 parents d6bf759 + defcb86 commit dd2d7fb

25 files changed

+517
-2
lines changed

app/templates/index.hbs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@
7070
<li class="list-unstyled" data-test-ember-data-3-link>
7171
<LinkTo @route="show" @models={{array "ember-data" "v3.x" }}>v3.x</LinkTo>
7272
</li>
73+
<li class="list-unstyled" data-test-ember-data-4-link>
74+
<LinkTo @route="show" @models={{array "ember-data" "v4.x" }}>v4.x</LinkTo>
75+
</li>
7376
</ul>
7477
</li>
7578
</ul>
76-
</div>
79+
</div>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: Deprecate Array Like
3+
until: '5.0'
4+
since: '4.7'
5+
displayId: ember-data:deprecate-array-like
6+
---
7+
8+
Deprecates Ember "Array-like" methods on `RecordArray` and `ManyArray`.
9+
10+
These are the arrays returned respectively by `store.peekAll()`, `store.findAll()` and hasMany relationships on instance of Model or `record.hasMany('relationshipName').value()`.
11+
12+
The appropriate refactor is to treat these arrays as native arrays and to use native array methods.
13+
14+
For instance, instead of:
15+
16+
```js
17+
users.firstObject;
18+
```
19+
20+
Use:
21+
22+
```js
23+
users[0];
24+
// or
25+
users.at(0);
26+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
displayId: ember-data:deprecate-early-static
3+
title: Deprecate Early Static
4+
until: '5.0'
5+
since: '4.7'
6+
---
7+
8+
This deprecation triggers if static computed properties or methods are triggered without looking up the record via the store service's `modelFor` hook. Accessing this static information without looking up the model via the store most commonly occurs when:
9+
10+
- using ember-cli-mirage (to fix, refactor to not use its auto-discovery of ember-data models)
11+
- importing a model class and accessing its static information via the import
12+
13+
Instead of:
14+
15+
```js
16+
import User from 'my-app/models/user';
17+
18+
const relationships = User.relationshipsByName;
19+
```
20+
21+
Do _at least_ this:
22+
23+
```js
24+
const relationships = store.modelFor('user').relationshipsByName;
25+
```
26+
27+
However, the much more future proof refactor is to not use `modelFor` at all, but instead to utilize the schema service for this static information:
28+
29+
```js
30+
const relationships = store
31+
.getSchemaDefinitionService()
32+
.relationshipsDefinitionFor({ type: 'user' });
33+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
displayId: ember-data:deprecate-errors-hash-to-array-helper
3+
title: Deprecate Errors Hash To Array Helper
4+
until: '5.0'
5+
since: '4.7'
6+
---
7+
8+
Deprecates `errorsHashToArray` `errorsArrayToHash` and `normalizeModelName`.
9+
10+
Users making use of these (already private) utilities can trivially copy them into their own codebase to continue using them, though we recommend refactoring to a more direct conversion into the expected errors format for the errors helpers.
11+
12+
For refactoring normalizeModelName we also recommend following the guidance in [RFC#740 Deprecate Non-Strict Types](https://github.com/emberjs/rfcs/pull/740).
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
displayId: ember-data:deprecate-errors-hash-to-array-helper
3+
title: Deprecate Errors Hash To Array Helper
4+
until: '5.0'
5+
since: '4.7'
6+
---
7+
8+
Deprecates `errorsHashToArray` `errorsArrayToHash` and `normalizeModelName`.
9+
10+
Users making use of these (already private) utilities can trivially copy them into their own codebase to continue using them, though we recommend refactoring to a more direct conversion into the expected errors format for the errors helpers.
11+
12+
For refactoring normalizeModelName we also recommend following the guidance in [RFC#740 Deprecate Non-Strict Types](https://github.com/emberjs/rfcs/pull/740).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
displayId: ember-data:deprecate-has-record-for-id
3+
title: Deprecate Has Record For Id
4+
until: '5.0'
5+
since: '4.5'
6+
---
7+
8+
Deprecates `store.hasRecordForId(type, id)` in favor of `store.peekRecord({ type, id }) !== null`.
9+
10+
Broadly speaking, while the ability to query for presence is important, a key distinction exists between these methods that make relying on `hasRecordForId` unsafe, as it may report `true` for a record which is not-yet loaded and un-peekable. `peekRecord` offers a safe mechanism by which to check for whether a record is present in a usable manner.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
displayId: ember-data:deprecate-instantiate-record-args
3+
title: Deprecate Instantiate Record Args
4+
until: '5.0'
5+
since: '4.12'
6+
---
7+
8+
Deprecates using the former 3rd and 4th arguments to `Store.instantiateRecord` which are now available as properties on the store.
9+
10+
Before:
11+
12+
```ts
13+
{
14+
instantiateRecord(identifier, createArgs, recordDataFor, notifications) {
15+
const cache = recordDataFor(identifier);
16+
}
17+
}
18+
```
19+
20+
After:
21+
22+
```ts
23+
{
24+
instantiateRecord(identifier, createArgs) {
25+
const { cache, notifications } = this;
26+
}
27+
}
28+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
displayId: ember-data:deprecate-model-reopen
3+
title: Deprecate Model Reopen
4+
until: '5.0'
5+
since: '4.7'
6+
---
7+
8+
For properties known ahead of time, instead of:
9+
10+
```ts
11+
class User extends Model {
12+
@attr firstName;
13+
}
14+
15+
User.reopen({ lastName: attr() });
16+
```
17+
18+
Extend `User` again or include it in the initial definition:
19+
20+
```ts
21+
class User extends Model {
22+
@attr firstName;
23+
@attr lastName;
24+
}
25+
```
26+
27+
For properties generated dynamically, consider registering a `SchemaDefinitionService` with the store, as such services are capable of dynamically adjusting their schemas, and utilize the `instantiateRecord` hook to create a Proxy based class that can react to the changes in the schema. Use `Foo extends Model` to extend your class instead.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
displayId: ember-data:deprecate-model-reopenclass
3+
title: Deprecate Model Reopenclass
4+
until: '5.0'
5+
since: '4.7'
6+
---
7+
8+
Instead of reopenClass, define `static` properties with native class syntax or add them to the final object.
9+
10+
Instead of:
11+
12+
```js
13+
User.reopenClass({ aStaticMethod() {} });
14+
```
15+
16+
Do this:
17+
18+
```js
19+
class User {
20+
static aStaticMethod() {}
21+
}
22+
```
23+
24+
Or, do this:
25+
26+
```js
27+
User.aStaticMethod = function () {};
28+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
displayId: ember-data:deprecate-non-strict-relationships
3+
title: Deprecate Non Strict Relationships
4+
until: '5.0'
5+
since: '4.7'
6+
---
7+
8+
Deprecates when belongsTo and hasMany relationships are defined without specifying whether the relationship is asynchronous.
9+
10+
The current behavior is that relationships which do not define this setting are aschronous (`{ async: true }`).
11+
12+
Instead of:
13+
14+
```js
15+
class Company extends Model {
16+
@hasMany('employee') employees;
17+
}
18+
19+
class Employee extends Model {
20+
@belongsTo('company') company;
21+
}
22+
```
23+
24+
Use:
25+
26+
```js
27+
class Company extends Model {
28+
@hasMany('employee', { async: true, inverse: 'company' }) employees;
29+
}
30+
31+
class Employee extends Model {
32+
@belongsTo('company', { async: true, inverse: 'employees' }) company;
33+
}
34+
```
35+
36+
Also deprecates when belongsTo and hasMany relationships are defined without specifying the inverse field on the related type.
37+
38+
The current behavior is that relationships which do not define this setting have their inverse determined at runtime, which is potentially non-deterministic when mixins and polymorphism are involved.
39+
40+
If an inverse relationship exists and you wish changes on one side to reflect onto the other side, use the inverse key. If you wish to not have changes reflected or no inverse relationship exists, specify `inverse: null`.
41+
42+
Instead of:
43+
44+
```js
45+
class Company extends Model {
46+
@hasMany('employee') employees;
47+
}
48+
class Employee extends Model {
49+
@belongsTo('company') company;
50+
}
51+
```
52+
53+
Use:
54+
55+
```js
56+
class Company extends Model {
57+
@hasMany('employee', { async: true, inverse: 'company' }) employees;
58+
}
59+
60+
class Employee extends Model {
61+
@belongsTo('company', { async: true, inverse: 'employees' }) company;
62+
}
63+
```
64+
65+
Instead of:
66+
67+
```js
68+
class Company extends Model {
69+
@hasMany('employee') employees;
70+
}
71+
class Employee extends Model {
72+
@attr name;
73+
}
74+
```
75+
76+
Use:
77+
78+
```js
79+
class Company extends Model {
80+
@hasMany('employee', { async: true, inverse: null }) employees;
81+
}
82+
83+
class Employee extends Model {
84+
@attr name;
85+
}
86+
```
87+
88+
And also deprecates when belongsTo and hasMany relationships are defined without specifying the inverse record's type.
89+
90+
Instead of
91+
92+
```js
93+
class Company extends Model {
94+
@hasMany() employees;
95+
}
96+
97+
class Employee extends Model {
98+
@belongsTo() company;
99+
}
100+
```
101+
102+
Use
103+
104+
```js
105+
class Company extends Model {
106+
@hasMany('employee', { async: true, inverse: 'company' }) employees;
107+
}
108+
109+
class Employee extends Model {
110+
@belongsTo('company', { async: true, inverse: 'employees' }) company;
111+
}
112+
```

0 commit comments

Comments
 (0)