我在将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"
}
]
}
我需要一个模型来拥有像这样的'items'属性:
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”总是“未定义”:|
提前致谢!
答案 0 :(得分:10)
parse
函数应返回要在模型上设置的属性哈希(请参阅documentation here)。所以你只需要:
parse: function (response) {
return response[0].items;
}