You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cookbook/overview.md
+3-6Lines changed: 3 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,8 @@
3
3
This “cookbook” section contains recipes for various scenarios you may encounter while working on your app or addon.
4
4
5
5
{% hint style="info" %}
6
-
7
-
Have an idea for an item that should fit here? [Open an issue for it!][gh-issue] We'd love to help you help us make this experience more awesome for everyone.
Have an idea for an item that should fit here? [Open an issue for it!](https://github.com/typed-ember/ember-cli-typescript/issues/new/choose) We'd love to help you help us make this experience more awesome for everyone.
11
7
{% endhint %}
12
8
13
-
- <LinkTo @route='docs.cookbook.working-with-route-models'>Working with route models</LinkTo>
-`Resolved<P>` says "if this is a promise, the type here is whatever the promise resolves to; otherwise, it's just the value"
25
-
-`ReturnType<T>` gets the return value of a given function
26
-
-`R['model']` (where `R` has to be `Route` itself or a subclass) uses TS's mapped types to say "the property named `model` on `R`
24
+
*`Resolved<P>` says "if this is a promise, the type here is whatever the promise resolves to; otherwise, it's just the value"
25
+
*`ReturnType<T>` gets the return value of a given function
26
+
*`R['model']`\(where `R` has to be `Route` itself or a subclass\) uses TS's mapped types to say "the property named `model` on `R`
27
27
28
-
Putting those all together, `ModelFrom<Route>` ends up giving you the resolved value returned from the `model` hook for a given route:
28
+
Putting those all together, `ModelFrom<Route>` ends up giving you the resolved value returned from the `model` hook for a given route:
29
29
30
-
```ts
30
+
```typescript
31
31
typeMyRouteModel=ModelFrom<MyRoute>;
32
32
```
33
33
34
34
## `model` on the controller
35
35
36
36
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:
Now, our controller’s `model` property will *always* stay in sync with the corresponding route’s model hook.
48
+
Now, our controller’s `model` property will _always_ stay in sync with the corresponding route’s model hook.
49
+
50
+
**Note:** this _only_ works if you do not mutate the `model` in either the `afterModel` or `setupController` hooks on the route! That's generally considered to be a bad practice anyway. If you do change the type there, you'll need to define the type in some other way and make sure your route's model is defined another way.
49
51
50
-
**Note:** this *only* works if you do not mutate the `model` in either the `afterModel` or `setupController` hooks on the route! That's generally considered to be a bad practice anyway. If you do change the type there, you'll need to define the type in some other way and make sure your route's model is defined another way.
Copy file name to clipboardExpand all lines: docs/ember-data/models.md
+27-30Lines changed: 27 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,23 @@
2
2
3
3
Ember Data models are normal TypeScript classes, but with properties decorated to define how the model represents an API resource and relationships to other resources. The decorators the library supplies "just work" with TypeScript at runtime, but require type annotations to be useful with TypeScript.
4
4
5
-
For an overview of using Ember's decorators with TypeScript, see <LinkTo @route='docs.ts.decorators'>our overview</LinkTo>.
5
+
For an overview of using Ember's decorators with TypeScript, see our overview.
6
6
7
7
## `@attr`
8
8
9
9
The type returned by the `@attr` decorator is whatever [Transform](https://api.emberjs.com/ember-data/release/classes/Transform) is applied via the invocation.
10
10
11
-
- If you supply no argument to `@attr`, the value is passed through without transformation.
12
-
13
-
- If you supply one of the built-in transforms, you will get back a corresponding type:
14
-
-`@attr('string')` → `string`
15
-
-`@attr(number)` → `number`,
16
-
-`@attr('boolean')` → `boolean`
17
-
-`@attr'date')` → `Date`
18
-
19
-
- If you supply a custom transform, you will get back the type returned by your transform.
11
+
* If you supply no argument to `@attr`, the value is passed through without transformation.
12
+
* If you supply one of the built-in transforms, you will get back a corresponding type:
13
+
*`@attr('string')` → `string`
14
+
*`@attr(number)` → `number`,
15
+
*`@attr('boolean')` → `boolean`
16
+
*`@attr'date')` → `Date`
17
+
* If you supply a custom transform, you will get back the type returned by your transform.
20
18
21
19
So, for example, you might write a class like this:
@@ -39,13 +37,13 @@ export default class User extends Model {
39
37
}
40
38
```
41
39
42
-
**Very important:** Even more than with decorators in general, you should be careful when deciding whether to mark a property as optional `?` or definitely present (no annotation): Ember Data will default to leaving a property empty if it is not supplied by the API or by a developer when creating it. That is: the *default* for Ember corresponds to an optional field on the model.
40
+
**Very important:** Even more than with decorators in general, you should be careful when deciding whether to mark a property as optional `?` or definitely present \(no annotation\): Ember Data will default to leaving a property empty if it is not supplied by the API or by a developer when creating it. That is: the _default_ for Ember corresponds to an optional field on the model.
43
41
44
-
The *safest* type you can write for an Ember Data model, therefore, leaves every property optional: this is how models *actually* behave. If you choose to mark properties as definitely present by leaving off the `?`, you should take care to guarantee that this is a guarantee your API upholds, and that ever time you create a record from within the app, *you* uphold those guarantees.
42
+
The _safest_ type you can write for an Ember Data model, therefore, leaves every property optional: this is how models _actually_ behave. If you choose to mark properties as definitely present by leaving off the `?`, you should take care to guarantee that this is a guarantee your API upholds, and that ever time you create a record from within the app, _you_ uphold those guarantees.
45
43
46
44
One way to make this safer is to supply a default value using the `defaultValue` on the options hash for the attribute:
47
45
48
-
```ts
46
+
```typescript
49
47
importModel, { attr } from'@ember-data/object';
50
48
51
49
exportdefaultclassUserextendsModel {
@@ -62,15 +60,14 @@ export default class User extends Model {
62
60
63
61
## `@belongsTo`
64
62
65
-
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }` (which it is by default).
66
-
67
-
- If the value is `true`, the type you should use is `DS.PromiseObject<Model>`, where `Model` is the type of the model you are creating a relationship to.
63
+
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
68
64
69
-
- If the value is `false`, the type is `Model`, where `Model` is the type of the model you are creating a relationship to.
65
+
* If the value is `true`, the type you should use is `DS.PromiseObject<Model>`, where `Model` is the type of the model you are creating a relationship to.
66
+
* If the value is `false`, the type is `Model`, where `Model` is the type of the model you are creating a relationship to.
70
67
71
68
So, for example, you might define a class like this:
importDSfrom'ember-data'; // NOTE: this is a workaround, see discussion below!
76
73
importUserfrom'./user';
@@ -85,24 +82,23 @@ export default class Post extends Model {
85
82
}
86
83
```
87
84
88
-
These are *type*-safe to define as always present, that is to leave off the `?` optional marker:
85
+
These are _type_-safe to define as always present, that is to leave off the `?` optional marker:
89
86
90
-
- accessing an async relationship will always return a `PromiseObject`, which itself may or may not ultimately resolve to a value—depending on the API response—but will always be present itself.
91
-
- accessing a non-async relationship which is known to be associated but has not been loaded will trigger an error, so all access to the property will be safe *if* it resolves at all.
87
+
* accessing an async relationship will always return a `PromiseObject`, which itself may or may not ultimately resolve to a value—depending on the API response—but will always be present itself.
88
+
* accessing a non-async relationship which is known to be associated but has not been loaded will trigger an error, so all access to the property will be safe _if_ it resolves at all.
92
89
93
-
Note, however, that this type-safety is not a guarantee of there being no runtime error: you still need to uphold the contract for non-async relationships (that is: loading the data first, or side-loading it with the request) to avoid throwing an error!
90
+
Note, however, that this type-safety is not a guarantee of there being no runtime error: you still need to uphold the contract for non-async relationships \(that is: loading the data first, or side-loading it with the request\) to avoid throwing an error!
94
91
95
92
## `@hasMany`
96
93
97
-
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }` (which it is by default).
94
+
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
98
95
99
-
- If the value is `true`, the type you should use is `DS.PromiseManyArray<Model>`, where `Model` is the type of the model you are creating a relationship to.
100
-
101
-
- If the value is `false`, the type is `EmberArray<Model>`, where `Model` is the type of the model you are creating a relationship to.
96
+
* If the value is `true`, the type you should use is `DS.PromiseManyArray<Model>`, where `Model` is the type of the model you are creating a relationship to.
97
+
* If the value is `false`, the type is `EmberArray<Model>`, where `Model` is the type of the model you are creating a relationship to.
102
98
103
99
So, for example, you might define a class like this:
104
100
105
-
```ts
101
+
```typescript
106
102
importModel, { hasMany } from'@ember-data/model';
107
103
importEmberArrayfrom'@ember/array';
108
104
importDSfrom'ember-data'; // NOTE: this is a workaround, see discussion below!
@@ -118,10 +114,11 @@ export default class Thread extends Model {
118
114
}
119
115
```
120
116
121
-
The same basic rules about the safety of these lookups as with `@belongsTo` apply to these types. The difference is just that in `@hasMany` the resulting types are *arrays* rather than single objects.
117
+
The same basic rules about the safety of these lookups as with `@belongsTo` apply to these types. The difference is just that in `@hasMany` the resulting types are _arrays_ rather than single objects.
122
118
123
119
## Importing `PromiseObject` and `PromiseManyArray`
124
120
125
-
There is no public import path in the [Ember Data Packages](https://emberjs.github.io/rfcs/0395-ember-data-packages.html) API for the `PromiseObject` and `PromiseManyArray` types. These types are slowly being disentangled from Ember Data and will eventually be removed. However, until they are, we need a way to refer to them. For *now*, the best option is to refer to them via the legacy `DS` import.
121
+
There is no public import path in the [Ember Data Packages](https://emberjs.github.io/rfcs/0395-ember-data-packages.html) API for the `PromiseObject` and `PromiseManyArray` types. These types are slowly being disentangled from Ember Data and will eventually be removed. However, until they are, we need a way to refer to them. For _now_, the best option is to refer to them via the legacy `DS` import.
126
122
127
123
In the future, they will become unnecesary, as the types will simply be `Promise<Model>` and `Promise<Array<Model>>`.
Copy file name to clipboardExpand all lines: docs/ember-data/overview.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
# Overview: Ember Data
2
2
3
-
In this section, we cover how to use TypeScript effectively with specific Ember Data APIs (anything you'd find under the `@ember-data` package namespace).
3
+
In this section, we cover how to use TypeScript effectively with specific Ember Data APIs \(anything you'd find under the `@ember-data` package namespace\).
4
+
5
+
We do _not_ cover general usage of Ember Data; instead, we assume that as background knowledge. Please see the Ember Data [Guides](https://guides.emberjs.com/release/models) and [API docs](https://api.emberjs.com/ember-data/release)!
4
6
5
-
We do *not* cover general usage of Ember Data; instead, we assume that as background knowledge. Please see the Ember Data [Guides](https://guides.emberjs.com/release/models) and [API docs](https://api.emberjs.com/ember-data/release)!
0 commit comments