ember-simple-auth deferReadiness直到加载用户

时间:2014-10-01 21:46:49

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

我正在使用ember-cli-simple-auth并扩展了会话对象以包含从/me端点检索到的currentUser。但是,当重新加载页面并且用户已登录时,在加载登录的用户信息之前会有一段延迟。我想推迟应用程序就绪,直到检索到用户。

我在custom-session初始化程序中有这个。

import Session from 'simple-auth/session';
export default {
  name: 'custom-session',
  initialize: function(container, app) {
    var _app = app;
    var SessionWithCurrentUser = Session.extend({
        currentUser: function() {
            var _this = this;
            return this.container.lookup('store:main').find('me', '').then(function(data){
                _app.advanceReadiness();
                _this.set('currentUser', data);
            }, function(data){
                console.log('failed');
                return data;
            });
        }.property()
    });

    container.register('session:withCurrentUser', SessionWithCurrentUser);
    app.deferReadiness();
  }
};

似乎永远不会调用advanceReadiness,因此应用永远不会加载。我是一个非常新的余烬,我仍然围绕容器,所以我不确定这是如何工作的。我做错了什么?

更新

export default {
  name: 'custom-session',
  initialize: function(container, app) {
    var _app = app;
    var SessionWithCurrentUser = Session.extend({
        currentUser: function() {
            var _this = this;
            return _this.container.lookup('store:main').find('me', '').then(function(data){
                _app.advanceReadiness();
                _this.set('currentUser', data);
            }, function(data){
                console.log('failed');
                return data;
            });
        }.property()
    });

    var session = SessionWithCurrentUser.create();
    container.register('session:withCurrentUser', session, { instantiate: false });
    app.deferReadiness();
    session.currentUser();
  }
};

从建议的答案中我将其更改为此,但这会导致错误undefined is not a function来自对session.currentUser()的调用。

堆栈跟踪

Uncaught TypeError: undefined is not a function app/initializers/custom-session.js:28
__exports__.default.initialize app/initializers/custom-session.js:28
(anonymous function) vendor.js:14807
visit vendor.js:15216
visit vendor.js:15214
visit vendor.js:15214
visit vendor.js:15214
DAG.topsort vendor.js:15312
Namespace.extend.runInitializers vendor.js:14804
Namespace.extend._initialize vendor.js:14689
Backburner.run vendor.js:12247
apply vendor.js:30430
run vendor.js:29048
runInitialize vendor.js:14488
fire vendor.js:3184
self.fireWith vendor.js:3296
jQuery.extend.ready vendor.js:3502
completed

1 个答案:

答案 0 :(得分:0)

您永远不会在初始化程序中调用currentUser方法。您需要将其更改为

var session = SessionWithCurrentUser.create()
container.register('session:withCurrentUser', session, { instantiate: false });
app.deferReadiness();
session.currentUser();

当然,在无法加载用户的情况下,您必须致电app.advanceReadiness();,否则应用程序永远不会启动。