-
-
Notifications
You must be signed in to change notification settings - Fork 518
Convert routing section to route components #2118
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d1b806
start converting a couple route files
locks b37e495
update all handlebars code blocks to gjs
mansona 9517054
add a zoey says cta to warn about --strict
mansona cf2d4be
Merge branch 'gjs' into gjs-routing
mansona f3575c8
update last references to hbs
mansona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,12 +55,14 @@ export default class FavoritePostsRoute extends Route { | |
|
|
||
| Now that data can be used in the `favorite-posts` template: | ||
|
|
||
| ```handlebars {data-filename=app/templates/favorite-posts.hbs} | ||
| {{#each @model as |post|}} | ||
| <div> | ||
| {{post.title}} | ||
| </div> | ||
| {{/each}} | ||
| ```gjs {data-filename=app/templates/favorite-posts.gjs} | ||
| <template> | ||
| {{#each @model as |post|}} | ||
| <div> | ||
| {{post.title}} | ||
| </div> | ||
| {{/each}} | ||
| </template> | ||
| ``` | ||
|
|
||
| Behind the scenes, what is happening is that the [route's controller](https://api.emberjs.com/ember/release/classes/Route/methods/setupController?anchor=setupController) receives the results of the model hook, and Ember makes the model hook results available to the template. Your app may not have a controller file for the route, but the behavior is the same regardless. | ||
|
|
@@ -133,22 +135,24 @@ export default class SongsRoute extends Route { | |
| In the `songs` template, we can specify both models and use the `{{#each}}` helper to display | ||
| each record in the song model and album model: | ||
|
|
||
| ```handlebars {data-filename=app/templates/songs.hbs} | ||
| <h1>Playlist</h1> | ||
| ```gjs {data-filename=app/templates/songs.gjs} | ||
| <template> | ||
| <h1>Playlist</h1> | ||
|
|
||
| <ul> | ||
| {{#each @model.songs as |song|}} | ||
| <li>{{song.name}} by {{song.artist}}</li> | ||
| {{/each}} | ||
| </ul> | ||
| <ul> | ||
| {{#each @model.songs as |song|}} | ||
| <li>{{song.name}} by {{song.artist}}</li> | ||
| {{/each}} | ||
| </ul> | ||
|
|
||
| <h1>Albums</h1> | ||
| <h1>Albums</h1> | ||
|
|
||
| <ul> | ||
| {{#each @model.albums as |album|}} | ||
| <li>{{album.title}} by {{album.artist}}</li> | ||
| {{/each}} | ||
| </ul> | ||
| <ul> | ||
| {{#each @model.albums as |album|}} | ||
| <li>{{album.title}} by {{album.artist}}</li> | ||
| {{/each}} | ||
| </ul> | ||
| </template> | ||
| ``` | ||
|
|
||
| ## Dynamic Models | ||
|
|
@@ -201,13 +205,9 @@ export default class PostRoute extends Route { | |
| } | ||
| ``` | ||
|
|
||
| Note that currently, if `model` is not specified, Ember will attempt | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for anyone following along, this was deprecated in ember@5 and is now removed so I just removed this paragraph 👍 |
||
| to automatically find a store and use it for lookup. This behavior | ||
| is a common source of confusion and will be removed in future Ember versions. | ||
|
|
||
| ### Linking to a dynamic segment | ||
|
|
||
| There are two ways to link to a dynamic segment from an `.hbs` template using [`<LinkTo>`](../../templates/links/). | ||
| There are two ways to link to a dynamic segment from an `.gjs` template using [`<LinkTo>`](../../templates/links/). | ||
| Depending on which approach you use, it will affect whether that route's `model` hook is run. | ||
| To learn how to link to a dynamic segment from within the JavaScript file, see the API documentation on | ||
| [`transitionTo`](https://api.emberjs.com/ember/release/classes/RouterService/methods/transitionTo?anchor=transitionTo) | ||
|
|
@@ -216,35 +216,48 @@ instead. | |
| When you provide a string or number to the `<LinkTo>`, the dynamic segment's `model` hook will run when the app transitions to the new route. | ||
mansona marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| In this example, `photo.id` might have an id of `4`: | ||
|
|
||
| ```handlebars {data-filename=app/templates/photos.hbs} | ||
| {{#each @model as |photo|}} | ||
| <LinkTo @route="photo" @model={{photo.id}}> | ||
| link text to display | ||
| </LinkTo> | ||
| {{/each}} | ||
| ```gjs {data-filename=app/templates/photos.gjs} | ||
| import { LinkTo } from '@ember/routing'; | ||
|
|
||
| <template> | ||
| {{#each @model as |photo|}} | ||
| <LinkTo @route="photo" @model={{photo.id}}> | ||
| link text to display | ||
| </LinkTo> | ||
| {{/each}} | ||
| </template> | ||
| ``` | ||
|
|
||
| However, if you provide the entire model context, the model hook for that URL segment will _not_ be run. | ||
| For this reason, many Ember developers choose to pass only ids to `<LinkTo>` so that the behavior is consistent. | ||
|
|
||
| Here's what it looks like to pass the entire `photo` record: | ||
|
|
||
| ```handlebars {data-filename=app/templates/photos.hbs} | ||
| {{#each @model as |photo|}} | ||
| <LinkTo @route="photo" @model={{photo}}> | ||
| link text to display | ||
| </LinkTo> | ||
| {{/each}} | ||
| ```gjs {data-filename=app/templates/photos.gjs} | ||
| import { LinkTo } from '@ember/routing'; | ||
|
|
||
| <template> | ||
| {{#each @model as |photo|}} | ||
| <LinkTo @route="photo" @model={{photo}}> | ||
| link text to display | ||
| </LinkTo> | ||
| {{/each}} | ||
| </template> | ||
| ``` | ||
|
|
||
| If you decide to pass the entire model, be sure to cover this behavior in your [application tests](../../testing/testing-application/). | ||
mansona marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| If a route you are trying to link to has multiple dynamic segments, like `/photos/4/comments/18`, be sure to specify all the necessary information for each segment: | ||
|
|
||
| ```handlebars | ||
| <LinkTo @route="photos.photo.comments.comment" @models={{array 4 18}}> | ||
| link text to display | ||
| </LinkTo> | ||
| ```gjs | ||
| import { LinkTo } from '@ember/routing'; | ||
| import { array } from '@ember/helper'; | ||
|
|
||
| <template> | ||
| <LinkTo @route="photos.photo.comments.comment" @models={{array 4 18}}> | ||
| link text to display | ||
| </LinkTo> | ||
| </template> | ||
| ``` | ||
|
|
||
| Routes without dynamic segments will always execute the model hook. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.