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 中删除 'parse' 函数,一切正常

4

1 回答 1

3

Backbone 期望从 parse 函数返回,尝试设置你的模型值而不是只返回你想要的 json,

parse: function(response){
  var historyList = new HistoryCollection();
  historyList.add(response.history);
  return {
    'id': response.id,
    'name': response.name,
    history: historyList
  };
}
于 2013-06-26T19:19:32.723 回答