我正在尝试创建具有 2 个相同类型的“hasMany”关系的主干关系模型,但我收到错误消息:“无法在“(myReverseRelationName)”上为模型 = 子创建关系 = 子:已被关系占用=孩子”。这是应该被允许的,还是我做错了?谢谢。
我创建了一个 jsFiddle,所以你们可以自己看看:http: //jsfiddle.net/Mu68f/5/
这是代码:
Animal = Backbone.RelationalModel.extend({
urlRoot: '/animal/',
});
AnimalCollection = Backbone.Collection.extend({
model: Animal
});
Zoo = Backbone.RelationalModel.extend({
relations: [
{
type: Backbone.HasMany,
key: 'largeAnimals',
relatedModel: Animal,
collectionType: AnimalCollection,
reverseRelation: {
key: 'livesIn',
includeInJSON: false
}
},
{
type: Backbone.HasMany,
key: 'smallAnimals',
relatedModel: Animal,
collectionType: AnimalCollection,
reverseRelation: {
key: 'livesIn',
includeInJSON: false
}
},
]
});
// initialize our zoo
var zoo = new Zoo({
largeAnimals: [{name: "Big Bill"}],
smallAnimals: [{name: "Pee Wee"}]
});
console.log(zoo);