[英]Can't get Injected Ember Service from Ember Controller - But works on models & adapters
[英]Ember - Can't access injected object in controller
我有一個用於Ember應用程序的初始化程序,並且已將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(並且此方法正常工作-我能夠在我的車把模板中訪問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')
});
這有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.