9
我在解析json到模型时遇到问题。Backbone 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”总是“未定义”:|
在此先感谢!
正好,我试过了,以及,仍然得到“未定义”:/ – asirgado
@asirgado我只注意到你的JSON似乎被包裹在一个阵列 - 是正确的?如果是这样,你需要改为'response [0] .items' ...? – McGarnagle
刚刚尝试过,它可以根据需要运行!谢谢!!如果我在阵列中获得更多的“物品”,它会起作用吗?谢谢! – asirgado