灰烬 - 无法访问控制器中的注入对象

时间:2015-01-30 05:57:58

标签: javascript ember.js

我的Ember应用程序有一个初始化程序,我已经注册/注入currentUser个对象到我的所有控制器和路由中。初始化程序似乎正在工作,因为我能够访问应用程序路径中的currentUser。但是,当我尝试访问ObjectController中的currentUser对象时,出现以下错误:

Uncaught TypeError: undefined is not a function

这里是Ember app的初始化程序:

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的参考问题吗?或者我的初始化程序设置有问题吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

你可能想写:

App.ConnectController = Ember.ObjectController.extend({
  currentUser: Ember.computed.alias('currentUser')
});

这有帮助吗?

相关问题