gpt4 book ai didi

javascript - Backbone 关系事件没有触发?

转载 作者:数据小太阳 更新时间:2023-10-29 05:39:06 25 4
gpt4 key购买 nike

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 抛出 addupdate 事件,即使没有什么是新的。

我不确定为什么会出现这些问题,任何帮助都会很好,谢谢!!!!

最佳答案

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

如果您没有看到这些更改事件,您可以检查一下您使用的是哪个版本的 backbone 和 backbone-relational 吗?

关于javascript - Backbone 关系事件没有触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7685791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com