主干关系事件未触发?
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
事件,即使没有什么是新的。
我不确定为什么会发生这些问题,任何帮助都会很棒,谢谢!!!!
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}]})
I have createModels on so themodel.get('subModels')
returns a collection of models.
Now if I pass changed subModel data into mymodel
themodel.set({subModels:[{changedstuff},{stuff},{stuff}]})
Shouldn't themodel
throw a change
event? It doesn't for me.
More so if I pass identical data into mymodel
themodel.set({subModels:[{samestuff},{samestuff},{samestuff}]})
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!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您通过设置新集合来重置整个关系,Backbone-relational 将(目前)仅替换整个集合,而不是检查差异。因此,它将为所有当前的
subModels
触发remove
事件,然后为每个新子模型触发add
事件。不过,我确实使用以下代码获取了
change
事件(如果发布的代码包含完整的示例,则会有所帮助;)这会产生以下输出:
如果您没有看到这些更改事件,您可以吗检查您正在使用哪个版本的主干和主干关系?
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 currentsubModels
, then fireadd
events for each new one.I do get
change
events though, with the following code (it would help if posted code contains a complete example though ;)This yields the following output:
If you don't see these change events, could you check which versions of backbone and backbone-relational you're using?