我正在尝试实现 BackboneRelational 并不断获得
“无法实例化多个 Backbone.RelationalModel
每种类型都有相同的 ID!”
class App.Models.User extends Backbone.RelationalModel
urlRoot : '/api/users'
idAttribute: 'id'
relations: [
type: Backbone.HasMany
key: 'plots'
relatedModel: 'App.Models.Plot'
collectionType: 'App.Collections.Plots'
includeInJSON: false
reverseRelation:
key: 'user_id',
includeInJSON: 'id'
]
class App.Models.Plot extends Backbone.RelationalModel
urlRoot : '/api/plots'
idAttribute: 'id'
如果我将其中一个模型切换为扩展 Backbone.Model,我可以实例化这两个模型,但我收到所有有关关系功能已损坏的警告。
我正在努力实现以下目标:
plot = new App.Models.Plot({id : 700})
plot.fetch()
plot.get('user')
我缺少什么?
“每个 id 一个模型”情况背后的总体思想是 Backbone Relational 使用数据存储(Backbone.Relational.store
)以消除对已加载模型的重复请求。
幸运的是,它还提供了几个帮手 https://github.com/PaulUithol/Backbone-relational#methods-on-the-type-itself帮助通过商店访问模型。您可以使用findOrCreate
您会发现附件中的方法App.Models.Plot
:
plot = App.Models.Plot.findOrCreate(700)
user = plot.get('user')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)