Skip to content

Commit 3ca1974

Browse files
committed
docs: de-addon-docs-ify working-with-route-models
1 parent 78d169a commit 3ca1974

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

docs/cookbook/working-with-route-models.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ We often use routes’ models throughout our application, since they’re a core
44

55
We can start by defining some type utilities to let us get the resolved value returned by a route’s model hook:
66

7-
<DocsSnippet @name='type-utils.ts' @title='my-app/lib/type-utils.ts' @showCopy={{true}} />
7+
```ts
8+
import Route from '@ember/routing/route';
9+
10+
/**
11+
Get the resolved type of an item.
12+
13+
- If the item is a promise, the result will be the resolved value type
14+
- If the item is not a promise, the result will just be the type of the item
15+
*/
16+
export type Resolved<P> = P extends Promise<infer T> ? T : P;
17+
18+
/** Get the resolved model value from a route. */
19+
export type ModelFrom<R extends Route> = Resolved<ReturnType<R['model']>>;
20+
```
821

922
How that works:
1023

@@ -22,7 +35,15 @@ type MyRouteModel = ModelFrom<MyRoute>;
2235

2336
We can use this functionality to guarantee that the `model` on a `Controller` is always exactly the type returned by `Route::model` by writing something like this:
2437

25-
<DocsSnippet @name='controller-with-model.ts' @title='my-app/controllers/controller-with-model.ts' @showCopy={{false}} />
38+
```ts
39+
import Controller from '@ember/controller';
40+
import MyRoute from '../routes/my-route';
41+
import { ModelFrom } from '../lib/type-utils';
42+
43+
export default class ControllerWithModel extends Controller {
44+
declare model: ModelFrom<MyRoute>;
45+
}
46+
```
2647

2748
Now, our controller’s `model` property will *always* stay in sync with the corresponding route’s model hook.
2849

tests/dummy/app/snippets/controller-with-model.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/dummy/app/snippets/type-utils.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)