每次转换时都会调用Ember.js路由挂钩

img*_*x64 16 ember.js

Ember.js中是否存在在每次转换时调用的路由挂钩,即使新路由与旧路由相同(例如,单击指向同一路径的顶级导航链接).

我尝试了activate,但它只被调用了一次,当我使用顶级导航到我已经在的相同路线时,它没有被再次调用.

示例jsFiddle:当我第一次单击"Test"时,activate会调用钩子,但是当我第二次单击它时,它不会.

kai*_*.ng 12

您可以在路由器中设置didTransition,这正是Ember为Google Analytics设置的方式.

App.Router.reopen({
 doSomething: function() {
    // do something here
    return;
  }.on('didTransition')
});
Run Code Online (Sandbox Code Playgroud)

请参阅此处的示例:http://emberjs.com/guides/cookbook/helpers_and_components/adding_google_analytics_tracking/


Mat*_*rte 6

Activate因为This hook is executed when the router enters the route...并且当你第二次点击该链接时,路由器没有做任何事情......因为没有进行任何转换(尽管它是"尝试").

http://emberjs.com/api/classes/Ember.Route.html#method_activate

我发现最好的方法是currentPath从控制器中观察.我将它用于路线之间的动画.

在您的应用程序控制器中,您可以执行以下操作:

currentPathChange: function () {
  switch(this.get('currentPath')){
    case 'test.index':
      this.doSomething();
      break;
    case 'test.new':
      this.doSomethingElse();
      break;
  }
}.observes('currentPath')
Run Code Online (Sandbox Code Playgroud)

您应该能够从应用程序控制器访问应用程序的几乎任何部分,因此我认为这是一个很好的"根钩".

示例:http://jsfiddle.net/mattblancarte/jxWjh/2/


mav*_*ein 5

你有没有考虑过这个钩子willTransition

http://emberjs.com/guides/routing/preventing-and-retrying-transitions/

App.SomeRoute = Ember.Route.extend({
  actions: {
    willTransition: function(transition) {
      // do your stuff
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 不,当新路线与旧路线相同时,不会调用它. (6认同)