我有一个带有嵌入式集合的主干模型,我使用“解析”功能:
var OrderModel = Backbone.Model.extend({
urlRoot:'/handlers/order',
defaults:{
'id':'',
'name': '',
'history': new HistoryCollection()
},
parse: function(response){
this.set({'id': response.id});
this.set({'name': response.name});
var historyList = new HistoryCollection();
historyList.add(response.history);
this.set({history: historyList});
}
})
收藏
var OrderCollection = Backbone.Collection.extend({
url: '/handlers/orders/',
model: OrderModel,
parse:function(response){
return response;
}
});
视图代码:
var c = new OrderCollection();
this.collection.fetch().complete(function(){
console.log(c);
});
我的服务器返回 JSON,模型未填充。但是,如果我从 OrderModel 中删除 'parse' 函数,一切正常