9

我无法将 json 解析为模型。

这是JSON:

[
{
    "name": "Douglas Crockford",
    "email": "example@gmail.com",
    "_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;
   }

});

然后我创建集合的新实例并获取,但我得到“response.items”总是“未定义”:|

提前致谢!

4

1 回答 1

10

parse函数应返回要在模型上设置的属性哈希(请参阅此处的文档)。所以你只需要:

parse: function (response) {
   return response[0].items;
}
于 2013-01-16T03:15:22.913 回答