Skip to content

Commit fc0f0f1

Browse files
committed
Deprecate implicit record loading in Ember Route
1 parent bec2958 commit fc0f0f1

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
Stage: Initial
3+
Start Date: 2021-11-14
4+
Release Date: Unreleased
5+
Release Versions:
6+
ember-source: vX.Y.Z
7+
ember-data: vX.Y.Z
8+
Relevant Team(s): Ember.js
9+
RFC PR: https://github.com/emberjs/rfcs/pull/774
10+
---
11+
12+
# Deprecate Implicit Record Loading in Routes
13+
14+
## Summary
15+
16+
This RFC seeks to deprecate and remove the default record loading behaviour on Ember's Route. By consequence, this will also deprecate and remove the default store on every Route. This behaviour is likely something you either know you have or do not know but it may be occuring in your app.
17+
18+
```js
19+
export default class PostRoute extends Route {
20+
beforeModel() {
21+
// do stuff
22+
}
23+
24+
afterModel() {
25+
// do stuff
26+
}
27+
}
28+
```
29+
30+
In this example, the `model` hook is not defined. However, Ember will attempt to try a few things before rendering this route's template.
31+
32+
1. If there is a `store` property on your route, it will attempt to call it's `find` method. Assuming you have `ember-data` installed, you may be expecting this. The arguments will be extracted from the params.
33+
a. For example, if a dynamic segment is `:post_id`, there exists logic to split on the underscore and and find a record of type `post`.
34+
35+
2. As a fallback, it will attempt to defined a `find` method and use your `Model` instance's find method to fetch. If no `find` method exists, an assertion is thrown.
36+
37+
## Motivation
38+
39+
If a user does not define a `model` hook, a side effect of going to the network has long confused developers. Users have come to associate `Route.model()` with a hook that returns `ember-data` models in the absense of explicit injection. While this can be true, it is not wholly true. New patterns of data loading are becoming accepted in the community including opting to fetch data in a Component or using different libraries.
40+
41+
By removing this behaviour, we will encourage developers to explicitly define what data is being fetched and from where.
42+
43+
## Detailed design
44+
45+
We will issue a deprecation to `findModel` notifying the user that if they want to continue this behaviour of attempting to fetch a resource implicitly, they should try and replicate with their own explicitly defined `model` hook. This will not remove returning the `transition` context when no `model` hook is defined.
46+
47+
## How we teach this
48+
49+
Most of this behaviour is lightly documented. Developers often come to this by mistake after some difficult searching. First, we will have to remove this sentence from the [docs](https://guides.emberjs.com/release/routing/defining-your-routes/#toc_dynamic-segments).
50+
51+
> The first reason is that Routes know how to fetch the right model by default, if you follow the convention.
52+
53+
A direct one to one replacement might look like this.
54+
55+
```js
56+
import { inject as service } from '@ember/service';
57+
58+
export default class PostRoute extends Route {
59+
// assuming you have ember-data installed
60+
@service store;
61+
62+
beforeModel() {
63+
// do stuff
64+
}
65+
66+
model({ post_id }) {
67+
return this.store.find('post', post_id);
68+
}
69+
70+
afterModel() {
71+
// do stuff
72+
}
73+
}
74+
```
75+
76+
## Alternatives
77+
78+
- Continue to provide fallback fetching behaviour but ensure no `assert` is called for users that neither a store nor a `Model` with a `find` method.
79+
80+
## Open Questions
81+
82+
## Related links and RFCs
83+
- [Deprecate defaultStore located at `Route.store`](https://github.com/emberjs/rfcs/issues/377)
84+
- [Pre-RFC: Deprecate implicit injections (owner.inject)](https://github.com/emberjs/rfcs/issues/508)
85+
- [Deprecate implicit record loading in routes](https://github.com/emberjs/rfcs/issues/557)

0 commit comments

Comments
 (0)