我已根据this文章
实施了身份验证因此,我在应用程序初始化时创建了一个App.Session
对象:
Ember.Application.initializer({
name: 'session',
initialize: function (container, application) {
App.Session = Ember.Object.extend({
init: function () {
this._super();
this.set('authToken', $.cookie('auth_token'));
this.set('authAccountId', $.cookie('auth_accountId'));
this.set('authAccountLanguage', $.cookie('auth_accountLanguage'));
},
authAccountIdChanged: function () {
var authAccountId = this.get('authAccountId');
$.cookie('auth_accountId', authAccountId);
//Load the actual account record from the server if the authAccountId is set
//Used to have for example the full name or other properties of the account
if (!Ember.isEmpty(authAccountId)) {
this.set('authAccount', this.store.find('account', authAccountId));
}
}.observes('authAccountId'),
...
我在authAccountId
有观察员;因此,每次更改accountId
(登录用户的id
)时,我都想检索该用户的所有详细信息(全名,首选项等)。
在Ember数据版本1.0.0之前,我正在使用:
this.set('authAccount', App.Account.find(authAccountId));
这很有效。现在我使用:this.set('authAccount', this.store.find('account', authAccountId));
我收到错误:Uncaught TypeError: Cannot call method 'find' of undefined
。同样在调试器中,this.get('store')
导致未定义。我的印象是该商店在Application.initializer
中不可用。有人可以帮助解决这个问题吗?
答案 0 :(得分:9)
您可以使用container.lookup('store:main')
这将返回在容器中注册的商店:
var store = container.lookup('store:main');
this.set('authAccount', store.find('account', authAccountId));