2013-01-16 10 views
9

モデルにjsonの解析に問題があります。ここでコレクションのモデルに対するバックボーンJSの解析json属性

はJSONです:

[ 
{ 
    "name": "Douglas Crockford", 
    "email": "[email protected]", 
    "_id": "50f5f5d4014e045f000002", 
    "__v": 0, 
    "items": [ 
     { 
      "cena1": "Cena1", 
      "cena2": "Cena2", 
      "cena3": Cena3, 
      "cena4": "Cena4", 
      "cena5": "Cena5", 
      "cena6": Cena6, 
      "_id": "50ee3e782a3d30fe020001" 
     } 
    ] 
} 

]

と私はこのような「アイテムの属性持っているモデルが必要:私が試した何

cena = new Model({ 
      cena1: "Cena1", 
      cena2: "Cena2", 
      ... 
}); 

を:

var cenaCollection = new Backbone.Collection.extend({ 
    model: Cenas, 
    url: '/orders', 

    parse: function (response) { 
     return this.model = response.items; 
    } 

}); 

次に、n ewのコレクションとフェッチのインスタンスですが、 "response.items"は常に "undefined"となります。

ありがとうございます!

答えて

10

parse関数は、モデルに設定する属性ハッシュを返す必要があります(documentation hereを参照)。だから、あなたは、単に必要があります

parse: function (response) { 
    return response[0].items; 
} 
+0

を正確に、私も、まだ「未定義」を取得することを試みた:/ – asirgado

+0

@asirgadoは、私はちょうどあなたのJSONは、アレイに包まれているように見える気づいた - ということは正しいでしょうか?もしそうなら、代わりに 'response [0] .items' ...? – McGarnagle

+0

ちょうどそれを試して、私の必要に応じて動作します!ありがとう!!私が配列の中にもっと多くの「アイテム」を持っていればそれはうまくいくのだろうか?ありがとう! – asirgado

関連する問題