Ember.js:涉及其他控制器的计算属性,在模型加载时不刷新

时间:2013-07-12 00:02:01

标签: ember.js ember-data

我有以下代码:

  App.UserController = App.EditableController.extend({
    needs: 'application',
    test: function() {
      return this.get('controller.application.me.email');
    }.property('controller.application.me.email'),
  });

  App.ApplicationController = Ember.Controller.extend({
    isPublic   : true,
    me         : null,
    init: function () {
      this.set('me', App.User.find(1));
      this._super();
    }
  });

但是一旦模型加载(从控制台),计算属性似乎不会更新:

> App.__container__.lookup('controller:Application').get('me.email')
"test@example.com"
> App.__container__.lookup('controller:User').get('test')
undefined

我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

假设您的App.EditableController类型为Ember.ObjectController,那么这应该有效:

App.UserController = Ember.ObjectController.extend({
  needs: 'application',
  // notice the we use here plural 'controllers' to have access to the
  // controllers defined with the 'needs' API
  contentBinding: 'controllers.application',
  test: function() {
    return this.get('content.me.email');
  }.property('content')
});

如果您的App.EditableController类型为Ember.Controller,则应执行此作业:

App.UserController = Ember.Controller.extend({
  needs: 'application',
  // notice the we use here plural 'controllers' to have access to the
  // controllers defined with the 'needs' API
  controllerBinding: 'controllers.application',
  test: function() {
    return this.get('controller.me.email');
  }.property('controller')
});

现在在控制台中执行App.__container__.lookup('controller:User').get('test')输出:

"test@example.com" // or whatever your example mail is

希望它有所帮助。