gpt4 book ai didi

json - Backbone JS将json属性解析为集合的模型

转载 作者:行者123 更新时间:2023-12-04 03:52:54 26 4
gpt4 key购买 nike

我在将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”“未定义”:|

提前致谢!

最佳答案

parse函数应返回要在模型上设置的属性哈希(请参见documentation here)。因此,您只需要:

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

关于json - Backbone JS将json属性解析为集合的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14350794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com