別の属性から配列を作成して返す計算されたプロパティがあります。この配列を で更新するとaddObject
、set
明らかに呼び出されないため、元の属性を更新できません。またはの元の属性も更新する方法はありますaddObject
かremoveObject
?
この例では、コンマで区切られた値の文字列から配列を作成します。
App.MyModel = DS.Model.extend({
someAttribute: DS.attr('string'),
computed: function(key, value) {
var computedArray,
someAttribute;
// getter
if (arguments.length === 1) {
someAttribute = this.get('someAttribute');
computedArray = description.split(',');
return Ember.A(computedArray);
}
// setter
else {
someAttribute = value.join(',');
this.set('someAttribute', someAttribute);
return value;
}
}.property('someAttribute')
});
計算されたプロパティを次のように更新すると、期待どおりに機能します。
>>> model.set('computed', ['turtles', 'all', 'the', 'way', 'down'])
['turtles', 'all', 'the', 'way', 'down']
>>> model.get('someAttribute')
"turtles,all,the,way,down"
しかし、今このようにすると、(予想通り) 同期されません:
>>> model.get('computed').addObject('oh yeah')
['turtles', 'all', 'the', 'way', 'down', 'oh yeah']
>>> model.get('someAttribute')
"turtles,all,the,way,down"
私の質問を一般化するには:計算されたプロパティが変更可能な場合、計算されたプロパティを計算に使用される属性と同期させることは可能ですか? そうでない場合、配列の回避策はありますか ( Ember.A()
)?
私は Ember を初めて使用し、内部でまだ知らないことが起こっている可能性がありますが、教えてください ;)