主干集合获取错误

时间:2012-11-30 10:34:09

标签: json backbone.js

我正在尝试从.json文件中获取一个集合。这是我的收藏代码

define(['jquery', 'underscore', 'backbone', 'vent'], function($, _, Backbone, vent) {
'use strict';

    var Wine = Backbone.Model.extend({
        urlRoot: "js/models/wines.json",
        defaults: {
            "id": null,
            "name":  "",
            "grapes":  "",
            "country":  "USA",
            "region":  "California",
            "year":  "",
            "description":  "",
            "picture":  ""
        }
    });

    return Backbone.Collection.extend({
        model: Wine,
        url: "js/models/wines.json",
    });
});

我正在拿这样的集合:

var _wine = new wineCollection();
_wine.fetch({
    success : function(data) {
        console.log("ON SUCCESS");
        console.log(data);
    },
    error: function(response) {
        console.log("ON ERROR");
        console.log(response);
    }
});

在控制台中,它始终显示“ON ERROR”消息:

ON ERROR
child
  _byCid: Object
  _byId: Object
  _callbacks: Object
  length: 0
  models: Array[0]
  __proto__: ctor

这是我的wines.json文件中的一项

{"id":"9","name":"BLOCK NINE","year":"2009","grapes":"Pinot Noir","country":"USA","region":"California","description":"With hints of ginger and spice, this wine makes an excellent complement to light appetizer and dessert fare for a holiday gathering.","picture":"block_nine.jpg"}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您是否尝试过在fetch方法中检查集合类(实际发送的是什么)。 您可能需要覆盖parse方法才能访问发送数据的内部部分。

例如:

Wine.Collection = Backbone.Collection.extend({

//we need to parse only the inner list
parse : function (response) {
    this.cursor = response.cursor;
    return response.list;
}

数组是内部列表: {list:[{item:one},{item:two}]}