我在尝试在应用程序中实现主干关系时遇到问题。
我尝试获取RelationalModel后出现此错误: 未捕获的TypeError:无法读取null的属性“id”
App.Models.Story = Backbone.RelationalModel.extend({
urlRoot: '/rest/v1/story/',
url:function(){
if (this.isNew()) {
return this.urlRoot;
} else {
return this.urlRoot + this.id + "/";
}
},
idAttribute: 'id',
relations: [{
type: Backbone.HasMany,
key: 'pieces',
createModels: true,
relatedModel: 'App.Models.Piece',
collectionType: 'App.Collections.Pieces',
includeInJSON: false,
reverseRelation: {
key: 'story'
}
}],
initialize: function(){
this.get('pieces').url = _.bind(function(){
return this.url() + "piece/"
}, this);
console.log("hello from story model");
}
});
这是集合
App.Collections.Stories = Backbone.Collection.extend({
url:'/rest/v1/story/',
model: App.Models.Story
});
我从我的API中检索数据
get all stories : GET /rest/v1/story/
get story no' 10 : GET /rest/v1/story/10/
get story pieces : GET /rest/v1/story/10/piece/
get piece no' 612 : GET /rest/v1/piece/612/
现在当我尝试
时var story = App.Models.Story({id:10});
story.fetch()
我收到此错误: 未捕获的TypeError:无法读取null的属性“id”
通过响应的Json成功创建了对象本身。
该错误破坏了RelationalModel设置关系。
感谢。