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

Simplifies the "All we want to do..." portion #281

Merged
merged 2 commits into from
Nov 29, 2018
Merged
Changes from all commits
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
30 changes: 14 additions & 16 deletions guides/v3.5.0/tutorial/routes-and-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,13 @@ Unlike the other route handlers we've made so far, the `index` route is special:
it does NOT require an entry in the router's mapping.
We'll learn more about why the entry isn't required later on when we look at [nested routes](../subroutes/) in Ember.

All we want to do when a user visits the root (`/`) URL is transition to `/rentals`.
To do this we will add code to our index route handler by implementing a route lifecycle hook,
called `beforeModel`.

Each route handler has a set of "lifecycle hooks", which are functions that are invoked at specific times during the loading of a page.
The [`beforeModel`](https://www.emberjs.com/api/ember/release/classes/Route/methods/beforeModel?anchor=beforeModel)
hook gets executed before the data gets fetched from the model hook, and before the page is rendered.
See [the next section](../model-hook/) for an explanation of the model hook.

In our index route handler, we'll call the [`replaceWith`](https://www.emberjs.com/api/ember/release/classes/Route/methods/beforeModel?anchor=replaceWith) function.
The `replaceWith` function is similar to the route's [`transitionTo()`](https://www.emberjs.com/api/ember/release/classes/Route/methods/transitionTo?anchor=transitionTo) function,
the difference being that `replaceWith` will replace the current URL in the browser's history,
while `transitionTo` will add to the history.
Since we want our `rentals` route to serve as our home page, we will use the `replaceWith` function.

In our index route handler, we add the `replaceWith` invocation to `beforeModel`.
All we want to do when a user visits the root (`/`) URL is transition to
`/rentals`. To do this we will add code to our index route handler by
implementing a route lifecycle hook called `beforeModel`.
Route lifecycle hooks are special methods that are called automatically when a route renders or data changes.
Inside, we'll call the
[`replaceWith`](https://www.emberjs.com/api/ember/release/classes/Route/methods/beforeModel?anchor=replaceWith)
function:

```javascript {data-filename="app/routes/index.js" data-diff="+4,+5,+6"}
import Route from '@ember/routing/route';
Expand All @@ -246,6 +237,13 @@ export default Route.extend({
});
```

The `replaceWith` function is similar to the route's
[`transitionTo()`](https://www.emberjs.com/api/ember/release/classes/Route/methods/transitionTo?anchor=transitionTo)
function, the difference being that `replaceWith` will replace the current URL
in the browser's history, while `transitionTo` will add to the history. Since we
want our `rentals` route to serve as our home page, we will use the
`replaceWith` function.

Now visiting the root route at `/` will result in the `/rentals` URL loading.

## Adding a Banner with Navigation
Expand Down