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')
会返回一组模型。
现在,如果我将更改后的subModel数据传递到mymodel
themodel.set({subModels:[{changedstuff},{stuff},{stuff}]})
不应themodel
举办change
活动吗?它不适合我。
如果我将相同的数据传递到mymodel
themodel.set({subModels:[{samestuff},{samestuff},{samestuff}]})
themodel.attributes.subModels
会抛出add
和update
个事件,即使没有什么是新事件。
我不确定为什么会出现这些问题,任何帮助都会很棒,谢谢!!!!
答案 0 :(得分: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 {}]
如果您没有看到这些更改事件,您可以查看您正在使用的主干和骨干关系版本吗?