我有以下JSON。
[
{
"FirstName": "John",
"SecondName": "Steve",
"Children":{
"results":[
{
"name":"Sue Anderson"
},
{
"name":"Jeff Anderson"
},
{
"name":"Mary Sanders"
}
]
}
},
{
"FirstName": "gary",
"SecondName": "Steve",
"Children":{
"results":[
{
"name":"nick"
},
{
"name":"Jerry"
},
{
"name":"tom"
}
]
}
}
]
我在骨干中有一个模型规范来代表它。
var FamilyData= Backbone.Model.extend({
defaults: {
FirstName: '',
SecondName:'',
Children:''
}
});
如何将这些子项作为数组存储在模型中?
答案 0 :(得分:1)
看起来像你想要的孩子。结果将被放入儿童。
所以在模型中添加一个parse方法覆盖:
var FamilyData= Backbone.Model.extend({
defaults: {
FirstName: '',
SecondName:'',
Children:''
},
parse: function(response) {
response.Children = response.Children.results
return response;
}
});
这会导致生成的FamilyData.Children包含JSON在Children.results中的内容