Javascript 主干集合在获取后具有空模型
我有一个带有嵌入式集合的主干模型,我使用“解析”功能:Javascript 主干集合在获取后具有空模型,javascript,rest,backbone.js,model,Javascript,Rest,Backbone.js,Model,我有一个带有嵌入式集合的主干模型,我使用“解析”功能: var OrderModel = Backbone.Model.extend({ urlRoot:'/handlers/order', defaults:{ 'id':'', 'name': '', 'history': new HistoryCollection() }, parse: function(response){ this.set({'id': respo
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'函数,则所有工作都会从parse函数中返回,请尝试设置模型值,而不是只返回所需的json
parse: function(response){
var historyList = new HistoryCollection();
historyList.add(response.history);
return {
'id': response.id,
'name': response.name,
history: historyList
};
}
主干需要从parse函数返回,请尝试设置模型值,而不是返回所需的json
parse: function(response){
var historyList = new HistoryCollection();
historyList.add(response.history);
return {
'id': response.id,
'name': response.name,
history: historyList
};
}