我有这样的模特:
window.Client = Backbone.RelationalModel.extend({
urlRoot: '/api/Client',
relations: [
{
type: Backbone.HasMany,
key: 'appointments',
relatedModel: 'Appointment',
autoFetch:true,
reverseRelation: {
key: 'client',
keySource: 'client_id',
includeInJSON: 'id',
autoFetch:true,
}
}
]
});
window.Appointment = Backbone.RelationalModel.extend({
urlRoot: '/api/Appointment',
});
'/api/Appointment'
响应中的JSON包含client
个对象数据,所以当我提取单个约会时,我希望它包含相关的Client
对象,但它没有。< / p>
var a= new Appointment({id:1});
a.fetch();
a.get('client');//null
a.fetchRelated('client')//this will make another request and get data that browser already have
是否可以使骨干关系自动设置父关系?
答案 0 :(得分:0)
fetch()是一种异步方法,它可能需要一些时间才能完成。在检查客户端是否已初始化之前,请尝试等待a.fetch完成:
a.fetch({
success: function () {
console.log(a.get('client'));
}
});
另外,你说/ api / Appointment返回的json包含客户端数据。如果是这种情况,则不要将autoFetch设置为true,因为不需要额外的提取。