【发布时间】:2013-08-06 20:42:06
【问题描述】:
我脑子里有这个模型结构:
var app = app || {};
// Caratteristica
app.Attribute = Backbone.Model.extend({
defaults: {
name: '',
selected: false
}
});
app.Attributes = Backbone.Collection.extend({
model: app.Attribute
});
// Tipo Caratteristica
app.AttributeCategory = Backbone.Model.extend({
defaults: {
name: '',
attributes: new app.Attributes()
}
});
app.AttributeCategories = Backbone.Collection.extend({
model: app.AttributeCategory,
url: '/ajax/attributes.cfm'
});
我在 '/ajax/attributes.cfm' 中的 API 会给我这样的响应:
[
{
"id": "1",
"name": "Type1",
"attributes": [
{
"id": "1",
"name": "Attribute1"
},
{
"id": "2",
"name": "Attribute2"
},
{
"id": "3",
"name": "Attribute3"
}
]
},
{
"id": "2",
"name": "Type2",
"attributes": [
{
"id": "1234",
"name": "Attribute1234"
},
{
"id": "2567",
"name": "Attribute2567"
}
]
}
]
我的问题是:这个 json 数据会被正确解析到我的嵌套数据结构中吗? 我的意思是我想最终在我的 app.AttributeCategories 集合中有两个 app.AttributeCategory 项。然后,这两项中的每一项都必须将其 attributes 属性填充为相应的 app.Attributes 集合。
如果答案是否定的,我将如何覆盖 parse() 函数以实现该结果?
【问题讨论】:
标签: backbone.js backbone-model backbone.js-collections