我有一个带有嵌入式集合的骨干模型,我使用'parse'功能:
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'函数,则所有工作
答案 0 :(得分:3)
Backbone期望从解析函数返回,尝试而不是设置模型值只返回你想要的json,
parse: function(response){
var historyList = new HistoryCollection();
historyList.add(response.history);
return {
'id': response.id,
'name': response.name,
history: historyList
};
}