Ember.js:在路由器中重定向到控制器中的某些条件? [英] Ember.js: redirect in router if certain condition is satisfied in controller?

查看:91
本文介绍了Ember.js:在路由器中重定向到控制器中的某些条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下路线:

  App.Router.map(function(){
this.resource('account',{path:'/'});
this.route('login',{path:'/ login'});
})

我当前的代码尝试访问路由中帐户控制器中的 loggedIn 属性:

  App.AccountRoute = Ember.Route.extend({
renderTemplate:function(controller){
var loggedIn = controller.get('loggedIn'); / / ERROR:controller undefined
if(!loggedIn){
this.transitionTo('login');
}
}
});

我应该在路由器中实现这个逻辑吗?为什么我的路由器中的控制器未定义?谢谢!

解决方案

以下是几个可能有助于您的创意:


  1. 您的控制器并不总是存在。它是由Ember第一次需要它创建的。您可以使用Chrome扩展程序进行Ember调试,以查看已创建哪些控制器。在你的情况下,它应该是可用的,因为你在 renderTemplate 钩子。一般来说,重定向应该在 beforeModel hook或 redirect hook之前完成:

      redirect:function(){
    if(!this.controller.get('loggedIn')){
    this.transitionTo '登录');
    }
    }


  2. 考虑将认证逻辑移动到Ember服务(示例)。 Ember中的一个服务只是一个扩展 Ember.Object 的类。您将能够将该服务注入所有的控制器和路由,所以它将始终可用。


  3. 更好的是:考虑使用优秀的 ember-simple-auth 。它将创建一个会话服务在您的应用程序的任何地方可用,所以你可以做的事情,如:

      //确保用户验证
    if(!this.get('session.isAuthenticated')){
    this.transitionTo('login');
    }

    甚至更好(因为你不想复制粘贴那些东西到处都是):

      //此路由现已验证! 
    App.AccountRoute = Ember.Route.extend(AuthenticatedRouteMixin,{
    ...
    }


还有很多其他很酷的东西!



另外,我看到你没有使用如果您对Ember感到更舒适,我建议您使用Ember CLI。Ember CLI是未来的的Ember,它有一个略有不同的语法,但很多伟大的事情。


Basically the objective is render the account page if user is logged in, otherwise redirect to a login page.

I have the following routes:

App.Router.map(function() {
  this.resource('account', { path: '/'});
  this.route('login', { path: '/login' });
})

My current code tries to access a loggedIn attribute in the account controller in the route:

App.AccountRoute = Ember.Route.extend({
  renderTemplate: function(controller) {
    var loggedIn = controller.get('loggedIn'); // ERROR: controller undefined
    if (!loggedIn) {
      this.transitionTo('login');
    }
  }
});

Should I implement this logic in the router? Why is the controller undefined in my route? Thanks!

解决方案

Here are a couple ideas that might help you:

  1. Your controller does not always exist. It is created by Ember when it needs it the first time. You can use the Chrome extension for Ember debugging to see which controllers are already created. In your case it should be available though since you are in the renderTemplate hook. In general, redirects should be done either in the beforeModel hook or the redirect hook:

    redirect: function () {
        if (!this.controller.get('loggedIn')) {
            this.transitionTo('login');
        }
    }
    

  2. Consider moving the authentication logic into an Ember service (example). A service in Ember is simply a class that extends Ember.Object. You will have the ability to inject that service into all your controllers and routes so it will be always available.

  3. Even better: consider using the excellent ember-simple-auth that handles both authentication and authorization. It will create a session service available everywhere in your app, so you will be able to do things such as:

    // Ensures the user is authenticated
    if (!this.get('session.isAuthenticated')) {
        this.transitionTo('login');
    }
    

    Or even better (since you don't want to copy paste that stuff everywhere):

    // This route is now authenticated!
    App.AccountRoute = Ember.Route.extend(AuthenticatedRouteMixin, { 
        ... 
    }
    

And many other cool things!

Also, I see that you are not using Ember CLI yet. I'd recommend it once you feel more comfortable with Ember. Ember CLI is the future of Ember, it comes with a slightly different syntax but lot of great things.

这篇关于Ember.js:在路由器中重定向到控制器中的某些条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆