2

我将 Tastypie 用于我的 RESTful api 和 Backbonejs 用于前端。with 模型工作正常,fetch但是,Backbone.Collection我似乎得到了错误的响应:

_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({

    });
}

);

4

1 回答 1

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);
            }
        });
    }
  });
}

一切正常。

于 2012-07-16T11:44:06.453 回答