简体   繁体   English

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

[英]ember-simple-auth deferReadiness until user is loaded

I am using ember-cli-simple-auth and have extended the session object to include the currentUser which is retrieved from the /me endpoint. 我正在使用ember-cli-simple-auth,并已扩展会话对象以包括从/me端点检索的currentUser。 However, when the page is reloaded and the user is logged in there is a delay until the logged in user information is loaded. 但是,当重新加载页面并登录用户时,在加载已登录的用户信息之前会有一个延迟。 I would like to defer the apps readiness until the user is retrieved. 我想推迟应用程序的准备工作,直到检索到用户为止。

I have this in a custom-session initializer. 我在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();
  }
};

It appears that advanceReadiness is never called so the app never loads. 似乎从来没有调用过advanceReadiness因此该应用程序永远也不会加载。 I am very new to ember and am still getting my head around the container so am unsure of how this works. 我对炭烬还很陌生,但仍然无法绕过容器,因此不确定其工作原理。 What am I doing wrong? 我究竟做错了什么?

Update 更新

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();
  }
};

From the answer suggested I changed it to this but this gives the error undefined is not a function coming from the call to session.currentUser() . 从建议的答案中,我将其更改为这个,但这给undefined is not a function的错误undefined is not a function来自对session.currentUser()的调用。

Stack trace 堆栈跟踪

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

You're never calling the currentUser method in the initializer. 您永远不会在初始化程序中调用currentUser方法。 You'd need to change that to 您需要将其更改为

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

Of course you'd have to call app.advanceReadiness(); 当然,您必须调用app.advanceReadiness(); in the case where the user could not be loaded as well, otherwise the app would never start up in that case. 如果无法同时加载用户,则在这种情况下该应用程序将永远无法启动。

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

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