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 ファイルの 1 つの項目です。

{"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 に答える