Skip to content

Commit e70da3f

Browse files
authored
Fix relationship links (#1854)
Closes 1840
1 parent 80fa6bf commit e70da3f

File tree

1 file changed

+24
-27
lines changed
  • tests/dummy/app/pods/docs/data-layer/relationships

1 file changed

+24
-27
lines changed

tests/dummy/app/pods/docs/data-layer/relationships/template.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ To define a to-one relationship, import the `belongsTo` helper and define a new
1010

1111
```js
1212
// mirage/models/blog-post.js
13-
import { Model, belongsTo } from 'ember-cli-mirage';
13+
import { Model, belongsTo } from "ember-cli-mirage";
1414

1515
export default Model.extend({
1616
author: belongsTo()
@@ -24,15 +24,15 @@ The `belongsTo` helper adds several new properties and methods to your models.
2424
In this case, our `blog-post` model would now have an `authorId` property, as well as some methods for working with the associated `author` model:
2525

2626
```js
27-
blogPost.authorId; // 1
28-
blogPost.authorId = 2; // updates the relationship
29-
blogPost.author; // Author instance
27+
blogPost.authorId; // 1
28+
blogPost.authorId = 2; // updates the relationship
29+
blogPost.author; // Author instance
3030
blogPost.author = anotherAuthor;
31-
blogPost.newAuthor(attrs); // new unsaved author
32-
blogPost.createAuthor(attrs); // new saved author (updates blogPost.authorId in memory only)
31+
blogPost.newAuthor(attrs); // new unsaved author
32+
blogPost.createAuthor(attrs); // new saved author (updates blogPost.authorId in memory only)
3333
```
3434

35-
Note that when a child calls `child.createParent`, the new parent is immediately saved to the `db`, but the child's foreign key is updated *on this instance only*, and is not immediately persisted to the database.
35+
Note that when a child calls `child.createParent`, the new parent is immediately saved to the `db`, but the child's foreign key is updated _on this instance only_, and is not immediately persisted to the database.
3636

3737
In other words, `blogPost.createAuthor` will create a new `author` record, insert it into the `db`, and update the `blogPost.authorId` in memory, but if you were to fetch the `blogPost` from the `db` again, the relationship would not be persisted.
3838

@@ -44,7 +44,7 @@ To define a to-many relationship, use the `hasMany` helper:
4444

4545
```js
4646
// mirage/models/blog-post.js
47-
import { Model, hasMany } from 'ember-cli-mirage';
47+
import { Model, hasMany } from "ember-cli-mirage";
4848

4949
export default Model.extend({
5050
comments: hasMany()
@@ -54,12 +54,12 @@ export default Model.extend({
5454
This helper adds a `commentIds` property to the `blogPost` model, as well as some methods for working with the associated `comments` collection:
5555

5656
```js
57-
blogPost.commentIds; // [1, 2, 3]
58-
blogPost.commentIds = [2, 3]; // updates the relationship
59-
blogPost.comments; // array of related comments
57+
blogPost.commentIds; // [1, 2, 3]
58+
blogPost.commentIds = [2, 3]; // updates the relationship
59+
blogPost.comments; // array of related comments
6060
blogPost.comments = [comment1, comment2]; // updates the relationship
61-
blogPost.newComment(attrs); // new unsaved comment
62-
blogPost.createComment(attrs); // new saved comment (comment.blogPostId is set)
61+
blogPost.newComment(attrs); // new unsaved comment
62+
blogPost.createComment(attrs); // new saved comment (comment.blogPostId is set)
6363
```
6464

6565
## Association options
@@ -72,13 +72,11 @@ For example,
7272

7373
```js
7474
// mirage/models/blog-post.js
75-
import { Model, belongsTo, hasMany } from 'ember-cli-mirage';
75+
import { Model, belongsTo, hasMany } from "ember-cli-mirage";
7676

7777
export default Model.extend({
78-
79-
author: belongsTo('user'),
80-
comments: hasMany('annotation')
81-
78+
author: belongsTo("user"),
79+
comments: hasMany("annotation")
8280
});
8381
```
8482

@@ -116,8 +114,8 @@ export default Model.extend({
116114

117115
// blog-post.js
118116
export default Model.extend({
119-
author: belongsTo('user'),
120-
reviewer: belongsTo('user')
117+
author: belongsTo("user"),
118+
reviewer: belongsTo("user")
121119
});
122120
```
123121

@@ -131,8 +129,8 @@ export default Model.extend({
131129

132130
// blog-post.js
133131
export default Model.extend({
134-
author: belongsTo('user', { inverse: 'blogPosts' }),
135-
reviewer: belongsTo('user', { inverse: null })
132+
author: belongsTo("user", { inverse: "blogPosts" }),
133+
reviewer: belongsTo("user", { inverse: null })
136134
});
137135
```
138136

@@ -168,14 +166,14 @@ Polymorphic associations have slightly different method signatures for their for
168166
```js
169167
let comment = schema.comments.create({ text: "foo" });
170168

171-
comment.buildCommentable('post', { title: 'Lorem Ipsum' });
172-
comment.createCommentable('post', { title: 'Lorem Ipsum' });
169+
comment.buildCommentable("post", { title: "Lorem Ipsum" });
170+
comment.createCommentable("post", { title: "Lorem Ipsum" });
173171

174172
// getter
175173
comment.commentableId; // { id: 1, type: 'blog-post' }
176174

177175
// setter
178-
comment.commentableId = { id: 2, type: 'picture' };
176+
comment.commentableId = { id: 2, type: "picture" };
179177
```
180178

181179
Has-many asssociations can also be polymorphic:
@@ -206,10 +204,9 @@ user.thingIds; // [ { id: 1, type: 'car' }, { id: 3, type: 'watch' }, ... ]
206204
user.thingIds = [ { id: 2, type: 'watch' }, ... ];
207205
```
208206

209-
210207
---
211208

212-
Be sure to check out the {{docs-link 'Schema' 'docs.api.item' 'modules/lib/orm/schema~Schema'}}, {{docs-link 'Model' 'docs.api.item' 'modules/lib/orm/model~Model'}} and {{docs-link 'Collection' 'docs.api.item' 'modules/lib/orm/collection~Collection'}} API docs to learn about all the available ORM methods.
209+
Be sure to check out the {{docs-link 'Schema' 'docs.api.item' 'modules/orm/schema~Schema'}}, {{docs-link 'Model' 'docs.api.item' 'modules/orm/model~Model'}} and {{docs-link 'Collection' 'docs.api.item' 'modules/orm/collection~Collection'}} API docs to learn about all the available ORM methods.
213210

214211
We'll also cover Serializers in these guides, where you'll learn how to customize the serialized forms of your models and collections to match your production API.
215212

0 commit comments

Comments
 (0)