あなたは(現時点では)新しいコレクション、バックボーン・リレーショナル意志を設定することにより、そのような全体の関係をリセットした場合だけではなく、違いをチェックする、コレクション全体を交換してください。だから、火、その後、現在のすべてのsubModels
ため、各新しいもののためにadd
イベントをremove
イベントを発生します。
私は(掲示コードはしかし完全な例が含まれている場合、それが役立つだろう;)次のコードでは、しかしchange
イベントを取得ん
var SubModel = Backbone.RelationalModel.extend({});
var TheModel = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'subModels',
relatedModel: SubModel,
reverseRelation: {
key: 'TheModel'
}
}]
});
themodel = new TheModel({subModels: [{ id: 5 },{id: 7},{id: 8}]})
themodel.bind('change', function() {
console.debug('change; args=%o', arguments);
});
themodel.bind('change:subModels', function() {
console.debug('change:subModels; args=%o', arguments);
});
themodel.bind('update:subModels', function() {
console.debug('update:subModels; args=%o', arguments);
});
themodel.bind('add:subModels', function() {
console.debug('add:subModels; args=%o', arguments);
});
themodel.bind('remove:subModels', function() {
console.debug('remove:subModels; args=%o', arguments);
});
console.debug('set new subModels');
themodel.set({subModels: [{ id: 5 },{id: 7},{id: 9}] })
これは、次のような出力が得られます。
set new subModels
change:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, [Object { id=5}, Object { id=7}, Object { id=9}], Object {}]
change; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, undefined]
remove:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object { _related={...}}]
remove:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object { _related={...}}]
remove:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object { _related={...}}]
add:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object {}]
add:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object {}]
add:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object {}]
update:subModels; args=[Object { _queue={...}, attributes={...}, _escapedAttributes={...}, more...}, Object { length=3, models=[3], _byId={...}, more...}, Object {}]
の場合あなたはあなたを使用していたバックボーンとバックボーン・リレーショナルのバージョンチェックすることもでき、これらの変更イベントが表示されませんか?
だから、私は、バックボーン・リレーショナルのcreateModels機能は、親モジュールに属性のさらなるセットすると、ネストされたモデルを更新しないことを考え出したと思います。それだけで彼らをclobbersし、新しいものを追加します。イベントの追加/削除だけがイベントを変更するのではなく、発生していたからです。また、データが同じときにすべてのイベントが発生するのもその理由です。少なくともこれが私の考えです。これが正しいか間違っているかどうか私に知らせてください。ありがとう! – fancy