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/ember-data/models.md
+11-20Lines changed: 11 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ For details about decorator usage, see [our overview of how Ember's decorators w
6
6
7
7
## `@attr`
8
8
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.
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. See [our overview of Transforms for more information](./transforms.md).
10
10
11
11
* If you supply no argument to `@attr`, the value is passed through without transformation.
12
12
* If you supply one of the built-in transforms, you will get back a corresponding type:
@@ -70,22 +70,21 @@ import type User from './user';
70
70
71
71
### `@belongsTo`
72
72
73
-
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
73
+
The type returned by the `@belongsTo` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
74
74
75
-
* 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.
75
+
* If the value is `true`, the type you should use is `AsyncBelongsTo<Model>`, where `Model` is the type of the model you are creating a relationship to.
76
76
* If the value is `false`, the type is `Model`, where `Model` is the type of the model you are creating a relationship to.
77
77
78
78
So, for example, you might define a class like this:
@@ -94,7 +93,7 @@ export default class Post extends Model {
94
93
95
94
These are _type_-safe to define as always present, that is to leave off the `?` optional marker:
96
95
97
-
* 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.
96
+
* accessing an async relationship will always return an `AsyncBelongsTo<Model>` object, which itself may or may not ultimately resolve to a value—depending on the API response—but will always be present itself.
98
97
* 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.
99
98
100
99
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!
@@ -103,32 +102,24 @@ Note, however, that this type-safety is not a guarantee of there being no runtim
103
102
104
103
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
105
104
106
-
* 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.
107
-
* If the value is `false`, the type is `EmberArray<Model>`, where `Model` is the type of the model you are creating a relationship to.
105
+
* If the value is `true`, the type you should use is `AsyncHasMany<Model>`, where `Model` is the type of the model you are creating a relationship to.
106
+
* If the value is `false`, the type is `SyncHasMany<Model>`, where `Model` is the type of the model you are creating a relationship to.
108
107
109
108
So, for example, you might define a class like this:
110
109
111
110
```typescript
112
-
importModel, { hasMany } from'@ember-data/model';
113
-
importEmberArrayfrom'@ember/array';
114
-
importDSfrom'ember-data'; // NOTE: this is a workaround, see discussion below!
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.
128
125
129
-
## Importing `PromiseObject` and `PromiseManyArray`
130
-
131
-
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.
132
-
133
-
In the future, they will become unnecesary, as the types will simply be `Promise<Model>` and `Promise<Array<Model>>`.
0 commit comments