Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Explain optional relations #1647

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs-website/docs/docs/Components.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ const EnhancedComment = enhance(Comment)

**Note** again that we can also pass `Relation` objects directly for convenience, skipping `.observe()`

### Reactive optional relations

Suppose the comment author is optional. If the comment has no author, the `comment.author_id` must be null. If `comment.author_id` has a value, the author record it refers to must be stored in the database, otherwise `withObservables` will throw an error that the record was not found.
mfbx9da4 marked this conversation as resolved.
Show resolved Hide resolved


### Reactive counters

Let's make a `<PostExcerpt>` component to display on a *list* of Posts, with only a brief summary of the contents and only the number of comments it has:
Expand Down Expand Up @@ -256,8 +261,8 @@ With the `switchMap` approach, you can do:
const enhance = withObservables(['post'], ({post}) => ({
post: post,
author: post.author,
contact: post.autor.observe().pipe(
switchMap(author => author ? autor.contact : of$(null))
contact: post.author.observe().pipe(
switchMap(author => author ? author.contact : of$(null))
)
}))
```
Expand Down
Loading