2

次のように、一連のチェーンされた計算されたプロパティがモデルに含まれています。

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')
});

すべてのオブジェクトに属性を設定する必要があるのは面倒です。より良い方法はありますか?

編集:

私たちが取り組んできたもう 1 つのアプローチは、callハンドルバー ヘルパーを定義することです。これにより、次のようなことが可能になります。

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

そして、モデルは次のようになります。

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

これは私にとって気分が良くなりますね。

4

0 に答える 0