【问题标题】:ember-simple-auth deferReadiness until user is loadedember-simple-auth deferReadiness 直到加载用户
【发布时间】:2014-11-26 21:01:16
【问题描述】:

我正在使用 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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 请看我的更新。这没有按预期工作
    • 请为异常添加堆栈跟踪
    • 就像我在更新中所说的那样调用session.currentUser()
    • 好的,你需要调试一下——没有什么能让我立即感到震惊。此外,您不应使用_this.container,而应使用container。也不需要定义额外的变量_app
    • 感谢您的帮助。我完全不知道从哪里开始调试这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多