我使用了ember-cli-simple-auth,并扩展了session对象以包括从/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发布于 2014-10-02 15:04:27
您永远不会在初始化器中调用currentUser方法。您需要将其更改为
var session = SessionWithCurrentUser.create()
container.register('session:withCurrentUser', session, { instantiate: false });
app.deferReadiness();
session.currentUser();当然,在用户无法加载的情况下,您必须调用app.advanceReadiness();,否则应用程序在这种情况下永远不会启动。
https://stackoverflow.com/questions/26151229
复制相似问题