【问题标题】:How do I access currentUser from Controllers?如何从控制器访问 currentUser?
【发布时间】:2014-06-12 08:29:09
【问题描述】:

这是我的初始化程序:

...
Ember.SimpleAuth.Session.reopen({
    currentUser: function() {
        var userId = this.get('user_id');

        if (!Ember.isEmpty(userId)) {
            return container.lookup('store:main').find('user', userId);
        }
    }.property('user_id')
});
...

控制器:

isAdmin: function() {
    var session = this.get('session.currentUser'),
        role = session.get('role'); // 'role' is undefined

    return role.get('name') == "Administrator";
}.property()

但是当我从模板中尝试时:

{{session.currentUser.role.name}}

效果很好。

我如何访问所有控制器甚至路由中的currentUser

【问题讨论】:

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


    【解决方案1】:

    我认为这是因为session.currentUser 是一个承诺。在你的控制器中试试这个:

    isAdmin: function() {
        return this.get('session.currentUser').then(function(r) {
          return r.get('role.name') == 'Administrator';
        });
    }.property()
    

    【讨论】:

      【解决方案2】:

      为什么不将 isAdmin 添加到 SimpleAuth 会话中:

      isAdmin: function() {
        return @get('current_user.role.name') == 'Administrator';
      }.property('current_user')
      

      那你应该可以了

      {{session.isAdmin}}
      

      在您的模板中。

      【讨论】:

        【解决方案3】:

        登录或注册时将用户信息保存在 codeigniter 会话中。

        然后跨页面访问当前登录用户信息表单会话。

        $userinfo = array(name=>'abc','userid'=>234);

        $this->session->set_userdata($userinfo );//用于存储用户信息

        $current = $this->session->userdata('userid'); //用于访问

        回显 $current;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-28
          • 2017-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-27
          • 2016-02-29
          • 1970-01-01
          相关资源
          最近更新 更多