主干关系的事件不触发? [英] Backbone relational events not firing?
问题描述
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('子模型')
返回模型的集合。
I have createModels on so themodel.get('subModels')
returns a collection of models.
现在如果我通过改变子模型数据转换成为MyModel
Now if I pass changed subModel data into mymodel
themodel.set({subModels:[{changedstuff},{stuff},{stuff}]})
不应该 themodel
抛出一个变更
事件?它并不适合我。
Shouldn't themodel
throw a change
event? It doesn't for me.
更何况,如果我通过相同的数据转换成为MyModel
More so if I pass identical data into mymodel
themodel.set({subModels:[{samestuff},{samestuff},{samestuff}]})
themodel.attributes.subModels
抛出添加
和更新
事件,即使没有什么是新的。
themodel.attributes.subModels
throws add
and update
events, even though nothing is new.
我不知道为什么这些问题发生,任何帮助将是巨大的,谢谢!!!!
I'm not sure why these issues are happening, any help would be great, thanks!!!!
推荐答案
如果您通过设置一个新的集合,骨干关系的意愿(目前)复位这样的整体关系只需更换而不是检查整个采集,差异。所以它会火删除
事件目前所有的子模型
,然后火添加
为每个新的事件。
If you reset a whole relation like that by setting a new collection, Backbone-relational will (at the moment) just replace the whole collection, instead of checking for differences. So it'll fire a remove
event for all current subModels
, then fire add
events for each new one.
我得到更改
事件虽然与下面code(如果贴code包含一个完整的例子,虽然它会帮助;)
I do get change
events though, with the following code (it would help if posted code contains a complete example though ;)
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 {}]
如果您没有看到这些变化的事件,你可以检查其骨干网和骨干网的关系,你正在使用的版本?
If you don't see these change events, could you check which versions of backbone and backbone-relational you're using?
这篇关于主干关系的事件不触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!