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

我在上创建了模型,因此themodel.get('subModels')返回一组模型。骨干关系事件没有解雇?


现在,如果我通过改变子模型数据转换成mymodel

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

不应该themodel抛出change事件?它不适合我。


更何况,如果我通过相同的数据到mymodel

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

themodel.attributes.subModels抛出addupdate事件,即使没有什么是新的。

我不知道为什么会发生这些问题,任何帮助将是伟大的,谢谢!

+0

所以我想我发现骨干关系的createModels功能不会更新嵌套模型,因为它们会在父模块的更多属性集上进行更新。它只是破坏它们并增加新的。所以这种情况发生的原因是因为只有添加/删除事件才会引发更改事件。这也是为什么所有这些事件都会在数据相同时触发的原因。至少这是我现在的想法,让我知道这是对还是错。谢谢! – fancy

回答

0

如果您通过设置新集合来重置整个关系,Backbone-relational将(现在)只替换整个集合,而不是检查差异。所以它会为所有当前的subModels发起remove事件,然后为每个新事件触发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 {}] 

如果你没有看到这些变化事件,你能检查你使用的骨干和骨干关系的哪个版本?

+0

我在Backbone 0.5.3和Backbone-Relational 0.4.0上有同样的问题 - 在父关系上添加和删除事件。变化什么都不做。 –