ember-simple-auth延迟直到用户加载 [英] ember-simple-auth deferReadiness until user is loaded

查看:157
本文介绍了ember-simple-auth延迟直到用户加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using ember-cli-simple-auth and have extended the session object to include the currentUser which is retrieved from the /me endpoint. 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.

我有一个自定义会话初始化器。

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 应用程序从不加载我很新奇,并且仍然让我的头靠在容器上,所以我不确定这是如何工作的。我做错了什么?

It appears that advanceReadiness is never called so the app never loads. 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?

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不是来自调用 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().

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


推荐答案

你永远调用初始化的 currentUser 方法。您需要将其更改为

You're never calling the currentUser method in the initializer. You'd need to change that to

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

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

Of course you'd have to call app.advanceReadiness(); in the case where the user could not be loaded as well, otherwise the app would never start up in that case.

这篇关于ember-simple-auth延迟直到用户加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆