1

我有以下代码:

  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

我错过了什么吗?

4

1 回答 1

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

希望能帮助到你。

于 2013-07-12T01:04:56.147 回答