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
+12-6Lines changed: 12 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,13 @@ 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
+
One should be certain to make use of [type-only imports](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html), available since TypeScript 3.8, and write `import type User from './user';`.
66
+
67
+
### `@belongsTo`
62
68
63
69
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
64
70
@@ -70,8 +76,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';
79
+
importtypeUserfrom'./user';
80
+
importtypeSitefrom'./site';
75
81
76
82
exportdefaultclassPostextendsModel {
77
83
@belongsTo('user')
@@ -89,7 +95,7 @@ These are _type_-safe to define as always present, that is to leave off the `?`
89
95
90
96
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
97
92
-
## `@hasMany`
98
+
###`@hasMany`
93
99
94
100
The type returned by the `@hasMany` decorator depends on whether the relationship is `{ async: true }`\(which it is by default\).
95
101
@@ -102,8 +108,8 @@ So, for example, you might define a class like this:
102
108
importModel, { hasMany } from'@ember-data/model';
103
109
importEmberArrayfrom'@ember/array';
104
110
importDSfrom'ember-data'; // NOTE: this is a workaround, see discussion below!
0 commit comments