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
+16-6Lines changed: 16 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,17 @@ export default class User extends Model {
58
58
}
59
59
```
60
60
61
-
## `@belongsTo`
61
+
## Relationships
62
+
63
+
Relationships between models in Ember Data rely on importing the related models, like `import User from './user';`. This, naturally, can cause a recursive loop, as `/app/models/post.ts` imports `User` from `/app/models/user.ts`, and `/app/models/user.ts` imports `Post` from `/app/models/post.ts`. Recursive importing triggers an [`import/no-cycle`](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-cycle.md) error from eslint.
64
+
65
+
To avoid these errors, use of [type-only imports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html), available since TypeScript 3.8:
66
+
67
+
```ts
68
+
importtypeUserfrom'./user';
69
+
```
70
+
71
+
### `@belongsTo`
62
72
63
73
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
64
74
@@ -70,8 +80,8 @@ So, for example, you might define a class like this:
importDSfrom'ember-data'; // NOTE: this is a workaround, see discussion below!
73
-
importUserfrom'./user';
74
-
importSitefrom'./site';
83
+
importtypeUserfrom'./user';
84
+
importtypeSitefrom'./site';
75
85
76
86
exportdefaultclassPostextendsModel {
77
87
@belongsTo('user')
@@ -89,7 +99,7 @@ These are _type_-safe to define as always present, that is to leave off the `?`
89
99
90
100
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!
91
101
92
-
## `@hasMany`
102
+
###`@hasMany`
93
103
94
104
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
95
105
@@ -102,8 +112,8 @@ So, for example, you might define a class like this:
102
112
importModel, { hasMany } from'@ember-data/model';
103
113
importEmberArrayfrom'@ember/array';
104
114
importDSfrom'ember-data'; // NOTE: this is a workaround, see discussion below!
0 commit comments