[英]Ember cli error with ember-simple-auth
我正在嘗試使用ember-simple-auth和自定義會話。 登錄后,當我嘗試在模板中訪問會話時,如下所示:
{{session.current_user.email}}
我收到以下錯誤:
Uncaught Error: Assertion Failed: Required contextualElement for view <Ember._HandlebarsBoundView:ember375> is missing
如果我重新加載頁面,錯誤就會消失。 此外,如果我使用未綁定的幫助程序,它會消失:
{{unbound session.current_user.email}}
我有以下代碼來在user_id更改時設置current_user:
import Session from 'simple-auth/session';
import Ember from 'ember';
export function initialize(container) {
Session.reopen({
setCurrentUser: function() {
var id = this.get('user_id');
var _this = this;
if(!Ember.isEmpty(id)) {
return container.lookup('store:main').find('user', id).then(function(user){
_this.set('current_user', user);
});
}
}.observes('user_id')
});
}
export default {
name: 'authentication',
before: 'simple-auth',
initialize: initialize
};
我究竟做錯了什么?
我會把這段代碼放在哪里? 初始化器? app/sessions/custom.js
?
export default Session.extend({
currentUser: function() {
var userId = this.get('user_id');
if (!Ember.isEmpty(userId)) {
return this.container.lookup('store:main').find('user', userId);
}
}.property('user_id')
});
然后在environment.js中我設置:
ENV['simple-auth'] = {
session: 'session:custom'
}
在初始化程序中我注冊自定義會話?
container.register('session:custom', Session);
將自定義會話移動到sessions/custom.js
。 還是一樣的錯誤:
我將在Session上定義一個currentUser
方法,該方法將用戶作為承諾返回:
export default Session.extend({
currentUser: function() {
var userId = this.get('user_id');
if (!Ember.isEmpty(userId)) {
return this.container.lookup('store:main').find('user', userId);
}
}.property('user_id')
});
此外,您應該更好地定義自己的客戶會話類,該類從Ember Simple Auth Session
類擴展而不是重新打開它。
類似於marcoow所說,但我們使用的是promise對象:
app/initializers/simple-auth-session.js
import Session from 'simple-auth/session';
import DS from 'ember-data';
export default {
name: 'simple-auth-custom-session',
before: 'simple-auth',
initialize: function(container) {
Session.reopen({
currentUser: function() {
return DS.PromiseObject.create({
promise: container.lookup('store:main').find('user', 'me')
});
}.property()
});
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.