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}]})

イベントをthemodel投げるべきではありませんか?changeそれは私にはありません。


同一のデータを渡すとさらにそうですmymodel

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

themodel.attributes.subModels新しいものは何もありませんが、スローaddとイベント。update

なぜこれらの問題が発生しているのかわかりません。助けていただければ幸いです。ありがとう!!!!

4

1 に答える 1

0

新しいコレクションを設定してそのようなリレーション全体をリセットすると、Backbone-relational は (現時点では) 違いをチェックするのではなく、コレクション全体を置き換えるだけです。したがって、remove現在のすべてのイベントが発生しsubModels、次にadd新しいイベントごとにイベントが発生します。

ただし、次のコードを使用してイベントを取得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 {}]

これらの変更イベントが表示されない場合は、使用しているバックボーンとバックボーン リレーショナルのバージョンを確認していただけますか?

于 2011-11-02T11:03:15.227 に答える