A very important feature of routes is redirection. This recipe will go over using the transitionTo
method.
In our route handler, we have special hooks called beforeModel
and afterModel
. We can use these hooks to perform actions before the model is loaded or after the model is loaded. The transitionTo
method can be used to redirect the application to different routes.
students
route:$ ember g route students
This will generate the students
route and template files.
// app/routes/students.js import Ember from 'ember'; export default Ember.Route.extend({ model() { return [1,2,3,4,5,6,7,8,9]; } });
routes/students.js
file again. This time, we'll add a before hook and a transition to it:// app/routes/students.js … beforeModel(){ this.transitionTo('teachers'); }
The transitionTo
method will redirect from one route to another. This option will redirect before the model is loaded and transition to the teacher's route.
afterModel()
hook:// app/routes/students.js … afterModel(){ this.transitionTo('teachers'); }
This will wait until the model is fully loaded before transitioning to the new route. You can check the loaded route before transitioning, if needed.