2

我正在使用 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从未调用过,因此该应用程序永远不会加载。我对 ember 还很陌生,但我仍然在摸索容器,所以不确定它是如何工作的。我究竟做错了什么?

更新

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
4

1 回答 1

0

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

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

当然,app.advanceReadiness();在无法加载用户的情况下,您也必须调用,否则在这种情况下应用程序将永远无法启动。

于 2014-10-02T07:04:26.907 回答