Ember:将mixin添加到除一个之外的每个路线

时间:2014-09-04 01:57:00

标签: ember.js ember-cli ember-simple-auth

在Ember-Cli应用程序中使用Ember-Simple-Auth,我正在尝试在我的应用程序中的几乎所有路径上进行身份验证。我不想在每条路线上使用AuthenticatedRouteMixin,因为我不想定义每条路线。所以我将mixin添加到 ApplicationRoute

然而,这会导致无限循环,因为显然登录路由从相同的 ApplicationRoute 扩展,因此现在受到保护。

除了 LoginRoute 之外,我如何在每个路线中包含此mixin?

1 个答案:

答案 0 :(得分:2)

我怀疑你在每条路线上都需要它,而不仅仅是你需要它作为经过验证的资源的大门。

App.Router.map(function(){
  this.route('login'); // doesn't need it
  this.resource('a', function(){ <-- need it here
    this.resource('edit');       <-- this is protected by the parent route
  });
  this.resource('b', function(){ <-- and here
    this.resource('edit');       <-- this is protected by the parent route
  });
});

或者你可以把它更深一层,然后创建一个包装所有东西的路线:

App.Router.map(function(){
  this.route('login'); // doesn't need it
  this.resource('authenticated', function(){ <-- put it here
    this.resource('a', function(){ <-- this is protected by the parent route
      this.resource('edit');       <-- this is protected by the grandparent route
    });
    this.resource('b', function(){ <-- this is protected by the parent route
      this.resource('edit');       <-- this is protected by the grandparent route
    });
  });
});