Ember - 无法访问控制器中的注入对象
Ember - Can't access injected object in controller
我的 Ember 应用程序有一个初始值设定项,并且我已经 registered/injected 一个 currentUser
对象到我的所有控制器和路由中。初始化程序似乎在工作,因为我能够在我的应用程序路由中访问 currentUser
。但是,当我尝试访问 ObjectController 中的 currentUser
对象时,出现以下错误:
Uncaught TypeError: undefined is not a function
这是 Ember 应用程序的初始化程序:
Ember.Application.initializer({
name: 'currentUserLoader',
initialize: function(container, application) {
application.deferReadiness();
Ember.$.ajax('http://localhost:5000', {
type: 'GET',
dataType: 'json',
success: function(data) {
var user = Ember.Object.create().setProperties(data[0]);
container.register('user:current', user, {instantiate: false, singleton: true});
container.injection('route', 'currentUser', 'user:current');
container.injection('controller', 'currentUser', 'user:current');
application.advanceReadiness();
},
error: function(err) {
console.log(err);
}
});
}
});
这是 ApplicationRoute(它工作正常 - 我可以在我的 Handlebar 模板中访问 currentUser
):
App.ApplicationRoute = Ember.Route.extend({
model: function() {
return Ember.RSVP.hash({
currentUser: this.get('currentUser')
});
}
});
这是我的 ObjectController(在这里,它不工作而是抛出错误。注意:我知道这个 Controller 现在看起来毫无意义,但这更像是注入的概念证明,因为我们可以似乎完全没有注射工作):
App.ConnectController = Ember.ObjectController.extend({
currentUser: this.get('currentUser'), // throws an undefined error
});
我到底做错了什么? this
的引用有问题吗?还是我的初始化设置有问题?
提前致谢!
你可能想写:
App.ConnectController = Ember.ObjectController.extend({
currentUser: Ember.computed.alias('currentUser')
});
这有帮助吗?
我的 Ember 应用程序有一个初始值设定项,并且我已经 registered/injected 一个 currentUser
对象到我的所有控制器和路由中。初始化程序似乎在工作,因为我能够在我的应用程序路由中访问 currentUser
。但是,当我尝试访问 ObjectController 中的 currentUser
对象时,出现以下错误:
Uncaught TypeError: undefined is not a function
这是 Ember 应用程序的初始化程序:
Ember.Application.initializer({
name: 'currentUserLoader',
initialize: function(container, application) {
application.deferReadiness();
Ember.$.ajax('http://localhost:5000', {
type: 'GET',
dataType: 'json',
success: function(data) {
var user = Ember.Object.create().setProperties(data[0]);
container.register('user:current', user, {instantiate: false, singleton: true});
container.injection('route', 'currentUser', 'user:current');
container.injection('controller', 'currentUser', 'user:current');
application.advanceReadiness();
},
error: function(err) {
console.log(err);
}
});
}
});
这是 ApplicationRoute(它工作正常 - 我可以在我的 Handlebar 模板中访问 currentUser
):
App.ApplicationRoute = Ember.Route.extend({
model: function() {
return Ember.RSVP.hash({
currentUser: this.get('currentUser')
});
}
});
这是我的 ObjectController(在这里,它不工作而是抛出错误。注意:我知道这个 Controller 现在看起来毫无意义,但这更像是注入的概念证明,因为我们可以似乎完全没有注射工作):
App.ConnectController = Ember.ObjectController.extend({
currentUser: this.get('currentUser'), // throws an undefined error
});
我到底做错了什么? this
的引用有问题吗?还是我的初始化设置有问题?
提前致谢!
你可能想写:
App.ConnectController = Ember.ObjectController.extend({
currentUser: Ember.computed.alias('currentUser')
});
这有帮助吗?