简体   繁体   English

登录Ember.js

[英]Login in Ember.js

I implemented a login with the help of embercasts and some posts of stackoverflow. 我在嵌入节目和一些stackoverflow的帮助下实现了登录。

It works but it feels bad, because of the use of ApplicationController together with LoginController . 它可以工作,但是感觉很糟糕,因为将ApplicationControllerLoginController一起使用。

The login should get me two things done. 登录应该使我完成两件事。

  1. hiding GUI elements, which aren't needed/available 隐藏不需要/不可用的GUI元素
  2. preventing the use of specific routes 防止使用特定路线

To hide the elements I needed to check the login state in my templates. 为了隐藏元素,我需要检查模板中的登录状态。 So I created a loggedIn attribute for the ApplicationController . 所以我为ApplicationController创建了一个loggedIn属性。 I don't know how to do this different, but it feels like a global (bad) variable. 我不知道该怎么做,但是感觉像是一个全局(坏)变量。

The AC also implements the logout, which is needed on the whole page, but still seems bad too, since it uses the LC in the background. AC还实现了注销,这在整个页面上都是必需的,但由于它在后台使用LC,因此看起来也很糟糕。

App.ApplicationController = Ember.Controller.extend({
    loggedIn: false,

    actions: {
        logout: function() {
            var loginCtrl = this.controllerFor('login');

            loginCtrl.set('token', null);

            this.set('loggedIn', false);

            this.transitionToRoute('landingpage');
        }
    }
});

This is the ApplicationRoute it also uses the LC for checking the state. 这是ApplicationRoute它也使用LC来检查状态。

App.ApplicationRoute = Ember.Route.extend({
    setupController: function(ctrl) {
        var loginCtrl = this.controllerFor('login');

        if(loginCtrl.get('token')) {
            ctrl.set('loggedIn', true);
        } else {
            ctrl.set('loggedIn', false);
        }
    }
});

Is there a better way to do this, without coupling the AC and LC? 有没有一种更好的方法,而无需将AC和LC耦合? Like getting the login state from LC in every template. 就像从每个模板的LC中获取登录状态一样。

Or should I just drop the LC and implement this logic in the AC, because it's needed everywhere? 还是我应该放弃LC并在AC中实现此逻辑,因为到处都需要它?

I agree with blessenm it is completely fine to call controllers from other controllers. 我同意blessenm,从其他控制器调用控制器完全可以。

In addition following the oop paradigm I suggest a little higher cohesion by specifying the login/logout/authentication responsibilities to the LoginController only. 另外,根据oop范例,我建议通过仅对LoginController指定登录/注销/身份验证职责来提高内聚性。

Thus the token would be part of the LC as well as its maintenance eg checking if logged in through a function, unsetting it through a logout function etc. Then there can be a base PrivateRoute (or reopen the Em.Route ), that will use this controller. 因此,令牌将成为LC及其维护的一部分,例如检查是否通过功能登录,通过注销功能取消设置等。然后可以有一个基本的PrivateRoute (或重新打开Em.Route ),它将使用这个控制器。 All other routes will extend from this route, if they require a logged in user. 如果所有其他路由都需要登录用户,则将从该路由扩展。 Logout action can be handled by ApplicationController but always call the logout function of LC . 注销动作可以由ApplicationController处理,但始终调用LC的注销函数。

Rough example of the routing given here, but no special controller had been used for brevity 这里给出了路由的粗略示例,但是为了简洁起见,没有使用特殊的控制器

most DRY way to redirect unauthenticated user to login screen in ember 大多数DRY方式将未经身份验证的用户重定向到ember中的登录屏幕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM