2012-07-14 64 views
2

我使用Tastypie为我的REST风格的api和Backbonejs的前端。在fetch配车型工作正常,但是,与Backbone.Collection我似乎获得错误的反应:Backbonejs集合返回错误的数据

_byCid: Object 
    _byId: Object 
    _callbacks: Object 
    _onModelEvent: function() { [native code] } 
    _removeReference: function() { [native code] } 
    length: 1 
    models: Array[1] 
     0: d 
     _callbacks: Object 
     _changed: false 
     _changing: false 
     _escapedAttributes: Object 
     _previousAttributes: Object 
     attributes: Object 
      cid: "c14" 
      collection: d 
       __proto__: o 
       length: 1 
       __proto__: Array[0] 
       __proto__: o 

下面是我收集:

define(
[ 
    'models/ad', 
], 
function(AdModel){ 
    return Backbone.Collection.extend({ 
     url: '/api/v1/ad', 
     model: AdModel, 

     initialize: function(){ 
      this.fetch({ 
       success: function(coll, resp){ 
        console.log(coll); 
       } 
      }); 
     } 
    }); 
} 

);

这里是我的模型:

define(
[], 
function(){ 
    return Backbone.Model.extend({ 

    }); 
} 

);

+0

不知道你在找什么...它似乎在创建一个集合并使用模型进行填充。你可以发布原始数据'fetch'返回和你期望它做什么? – 2012-07-15 02:02:42

+0

这似乎是与模型阵列填充它,这不是我应该期望从'集合',对不对?返回的数据中应该有一个集合属性,对吗?如何从控制台btw复制原始数据? – 2012-07-15 08:30:21

回答

1

所有我必须改变的是我的Collection.parse方法。

define(
    [ 
    'models/ad', 
    ], 
function(AdModel){ 
    return Backbone.Collection.extend({ 
     url: '/api/v1/ad', 
     model: AdModel, 
     parse: function(data){ 
      return data.objects; 
     }, 
     initialize: function(){ 
     this.fetch({ 
      success: function(coll, resp){ 
       console.log(coll); 
      } 
     }); 
    } 
    }); 
} 

和一切工作正常。