在ember中,对于依赖于模型和模型的计算属性的正确模式是什么?控制器属性?

时间:2013-07-10 16:18:32

标签: ember.js

我们的模型中有一系列链式计算属性,如下所示:

App.MyModel = Em.Object.extend({
  value0: 2
  value1: 5,
  value2: function() {
    return this.get('value1') + this.get('value0');
  }.property('value1', 'value0'),
  value3: function() {
    return this.get('value2') * this.get('value0');
  }.property('value2', 'value0')
});

我们现在希望将value0属性绑定到ArrayController中的值。实现这一目标的最正确方法是什么?

这是我们现在使用的方法:

App.MyController = Em.ArrayController.extend({
  value0: function(key, value) {
    if (arguments.length > 1) {
      this.setEach('value0', value);
    }
    return this.get('firstObject.value0');
  }.property('firstObject.value0')
});

需要在每个对象上设置属性感觉很麻烦。还有更好的方法吗?

编辑:

我们一直在努力的另一种方法是定义一个call把手助手,它可以允许这样的事情:

{{#each controller}}
   <li>{{call someModelMethod someControllerProperty}}</li>
{{/each}}

然后模型看起来像这样:

App.MyModel = Em.Object.extend({
  value1: 5,
  someModelMethod: function(value0) {
    return this.get('value1') * value0;
  }
});

这对我来说感觉更好,想法?

0 个答案:

没有答案
相关问题