一直在尝试启动并运行一段时间。基本上我有一个rest api作为返回json的后端,一切都很好。
我遇到的问题是骨干关系。我确定我刚刚出了点问题,但我已经在网上搜索了一段时间,但找不到任何帮助。
我试图在模型“专业”和“武器”之间建立一个有很多关系。这是我的代码:
Profession = Backbone.RelationalModel.extend({
urlRoot: '../api/professions',
relations: [{
type: Backbone.HasMany,
key: 'weapons',
relatedModel: 'Weapon',
collectionType: 'Weapons',
reverseRelation: {
key: 'profession',
includeInJSON: 'id',
keySource: 'profession_id'
}
}]
});
Weapon = Backbone.RelationalModel.extend({
urlRoot: '../api/weapons'
});
Weapons = Backbone.Collection.extend({
model: Weapon,
url: function(models){
return '../api/weapons';
}
});
对这些 api 端点的调用返回:
{name: "Profession1", id: 1}
[{name: "Weapon1", profession_id: 1}, {name: "Weapon2", profession_id: 1}]
所以,如果我理解正确,profession.fetchRelated('weapons')
应该向武器集合的 url 发送一个 httprequest,并拉出一个professional_id 为 1 的对象。但是当我运行时没有任何反应profession.fetchRelated('weapons')