Transitioning and Redirecting

过渡与重定向

Calling transitionTo from a route or transitionToRoute from a controller will stop any transition currently in progress and start a new one, functioning as a redirect. transitionTo takes parameters and behaves exactly like the link-to helper:

在路由中调用transitionTo或者在控制器中调用transitionToRoute,将停止当前正在进行的过渡,并开启一个新的,这也用作重定向。transitionTo具有参数,其行为与link-to助手相同。

  • If you transition into a route without dynamic segments that route’s model hook will always run.

  • 如果过渡到一个没有动态段的路由,路由的model钩子始终都会运行。

  • If the new route has dynamic segments, you need to pass either a model or an identifier for each segment. Passing a model will skip that segment’s model hook. Passing an identifier will run the model hook and you’ll be able to access the identifier in the params. See Links for more detail.

  • 如果路由具有动态段,那么需要传入一个模型或者一个标识符给每个段。传入一个模型不会调用model钩子,传入一个标识符会触发model钩子,标识符可以通过参数获取。详细内容请查看链接

Before the model is known

在获取模型之前

If you want to redirect from one route to another, you can do the transition in the beforeModel hook of your route handler.

如果希望从一个路由重定向到另一个路由,可以在beforeModel钩子中进行过渡。

  1. App.Router.map(function() {
  2. this.resource('posts');
  3. });
  4. App.IndexRoute = Ember.Route.extend({
  5. beforeModel: function() {
  6. this.transitionTo('posts');
  7. }
  8. });

After the model is known

在获取模型之后

If you need some information about the current model in order to decide about the redirection, you should either use the afterModel or the redirect hook. They receive the resolved model as the first parameter and the transition as the second one, and thus function as aliases. (In fact, the default implementation of afterModel just calls redirect.)

如果需要从当前模型中获取重定向的信息来决定跳转到哪里,可以使用afterModelredirect这两个钩子来实现。afterModelredirect的第一个参数都是路由的模型,过渡对象作为第二个参数,两个钩子本质是一样的。(实际上afterModel钩子的缺省实现只是对redirect钩子的调用)。

  1. App.Router.map(function() {
  2. this.resource('posts');
  3. this.resource('post', { path: '/post/:post_id' });
  4. });
  5. App.PostsRoute = Ember.Route.extend({
  6. afterModel: function(posts, transition) {
  7. if (posts..get('length') === 1) {
  8. this.transitionTo('post', posts[0]);
  9. }
  10. }
  11. });

When transitioning to the PostsRoute if it turns out that there is only one post, the current transition will be aborted in favor of redirecting to the PostRoute with the single post object being its model.

当过渡到PostsRoute路由时,如果发现只有一篇文章,那么当前的过渡会被取消,并重定向到PostRoute路由,来显示这一篇文章。

Based on other application state

基于应用的其他状态

You can conditionally transition based on some other application state.

条件过渡可以基于应用的一些其他的状态。

  1. App.Router.map(function() {
  2. this.resource('topCharts', function() {
  3. this.route('choose', { path: '/' });
  4. this.route('albums');
  5. this.route('songs');
  6. this.route('artists');
  7. this.route('playlists');
  8. });
  9. });
  10. App.TopChartsChooseRoute = Ember.Route.extend({
  11. beforeModel: function() {
  12. var lastFilter = this.controllerFor('application').get('lastFilter');
  13. this.transitionTo('topCharts.' + (lastFilter || 'songs'));
  14. }
  15. });
  16. // Superclass to be used by all of the filter routes below
  17. App.FilterRoute = Ember.Route.extend({
  18. activate: function() {
  19. var controller = this.controllerFor('application');
  20. controller.set('lastFilter', this.templateName);
  21. }
  22. });
  23. App.TopChartsSongsRoute = App.FilterRoute.extend();
  24. App.TopChartsAlbumsRoute = App.FilterRoute.extend();
  25. App.TopChartsArtistsRoute = App.FilterRoute.extend();
  26. App.TopChartsPlaylistsRoute = App.FilterRoute.extend();

In this example, navigating to the / URL immediately transitions into the last filter URL that the user was at. The first time, it transitions to the /songs URL.

在上面这个例子中,用户浏览到/网址的时候会被立即跳转到用户曾经访问过的最后过滤网址。第一次访问/的时候,会被跳转到songs网址。

Your route can also choose to transition only in some cases. If the beforeModel hook does not abort or transition to a new route, the remaining hooks (model, afterModel, setupController, renderTemplate) will execute as usual.

路由还可以选择在特定的情况下才跳转。如果beforeModel钩子没有跳转到新的路由,剩下的钩子(model, afterModel,setupController, renderTemplate)还会照常执行。