あなたがやろうとしていることは意味がありません。あなたdefaultsは単一のオブジェクトを保持する配列です:
defaults: {
attrArray: [
{ item_id: '', type: '', name: '' }
]
},
属性オブジェクトのリストを保持する場合は、配列を使用します。しかし、属性オブジェクトのリストがある場合、どれを参照するitem_idことを期待attrArray['item_id']しますか?これは常にデフォルト値に初期化され、モデルの初期データの一部としてattrArray誰も送信しないと想定していますか?attrArrayもしそうなら、あなたはこのようなものがもっと欲しいでしょう:
// Use a function so that each instance gets its own array,
// otherwise the default array will be attached to the prototype
// and shared by all instances.
defaults: function() {
return {
attrArray: [
{ item_id: '', type: '', name: '' }
]
};
},
initialize: function() {
// get will return a reference to the array (not a copy!) so
// we can modify it in-place.
this.get('attrArray')[0]['item_id'] = this.cid;
}
特別な処理を必要とする配列属性でいくつかの問題が発生することに注意してください。
get('attrArray')モデル内にある配列への参照を返すため、その戻り値を変更するとモデルが変更されます。
a = m.get('attrArray'); a.push({ ... }); m.set('attrArray', a)期待どおりに機能しない、配列が変更されたことsetに気付かない(変更されていないため、a == a結局のところtrueである)ため、との間のどこかに"change"クローンを作成しない限り、イベントを取得できません。attrArraygetset