1

我正在尝试从 .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"}

我究竟做错了什么?

4

1 回答 1

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}]}

于 2012-11-30T11:56:07.640 回答