2013-06-26 122 views
0

後,我有一個嵌入式收集骨幹模型,我使用「解析」功能:骨幹集合了空模型獲取

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刪除「解析」功能的所有作品

回答

3

骨幹預計從剖析功能的迴歸,可以嘗試設置你的模型值,而不是隻返回你想要的JSON,

parse: function(response){ 
    var historyList = new HistoryCollection(); 
    historyList.add(response.history); 
    return { 
    'id': response.id, 
    'name': response.name, 
    history: historyList 
    }; 
} 
+0

謝謝,安德魯!有用! – Linkorn