6
class TheModel extends Backbone.RelationalModel 
    relations:[ 
     type: Backbone.HasMany 
     key: 'subModels' 
     relatedModel: SubModel 
     collectionType: SubModels 
     reverseRelation: 
      key: 'TheModel' 
    ] 

themodel = new the TheModel({subModels:[{#stuff},{#stuff},{#stuff}]}) 

私はにcreateModelsを持っていますので、themodel.get('subModels')はモデルのコレクションを返します。私はmymodelバックボーンのリレーショナルイベントは起動しませんか?

themodel.set({subModels:[{changedstuff},{stuff},{stuff}]}) 

に変更サブモデルのデータを渡す場合


themodelchangeイベントを投げるべきではないでしょうか。それは私のためではありません。


詳しいので、私は何も新しいものでなくても、 mymodel

themodel.set({subModels:[{samestuff},{samestuff},{samestuff}]}) 

themodel.attributes.subModelsaddupdateイベントをスローに同一のデータを渡す場合。

私は、おかげで、これらの問題が起こっている理由は、任意のヘルプは素晴らしいことだか分からない!!!!

+0

だから、私は、バックボーン・リレーショナルのcreateModels機能は、親モジュールに属性のさらなるセットすると、ネストされたモデルを更新しないことを考え出したと思います。それだけで彼らをclobbersし、新しいものを追加します。イベントの追加/削除だけがイベントを変更するのではなく、発生していたからです。また、データが同じときにすべてのイベントが発生するのもその理由です。少なくともこれが私の考えです。これが正しいか間違っているかどうか私に知らせてください。ありがとう! – fancy

答えて

0

あなたは(現時点では)新しいコレクション、バックボーン・リレーショナル意志を設定することにより、そのような全体の関係をリセットした場合だけではなく、違いをチェックする、コレクション全体を交換してください。だから、火、その後、現在のすべての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 {}] 

の場合あなたはあなたを使用していたバックボーンとバックボーン・リレーショナルのバージョンチェックすることもでき、これらの変更イベントが表示されませんか?

+0

は私がバックボーン0.5.3およびバックボーン・リレーショナル0.4.0で同じ問題を抱えている - 親関係にイベント火災を追加し、削除します。変更は何もしません。 –

関連する問題