i have not worked with Backbone relational before. But I would construct the structure, using 2 models and having a parse
method on the model and passing the main collection.
var navi = {
"mainLinkLabel": "Home",
"mainLinkHref": "/home.html",
"sublinks": [{
"sublinkLabel": "subHome1",
"sublinkHref": "/home/home1.html"
}, {
"sublinkLabel": "subHome2",
"sublinkHref": "/home/home2.html"
}, {
"sublinkLabel": "subHome3",
"sublinkHref": "/home/home3.html"
}]
};
var MainModel = Backbone.Model.extend({
defauts: {
"mainLinkLable": "default Lable",
"mainLinkHref": "#"
},
parse: function(response) {
if(response.sublinks) {
this.subCollection = new SubCollection(response.sublinks || null, { parse: true });
}
delete response.sublinks;
return response;
}
})
var SubModel = Backbone.Model.extend({
defaults: {
"sublinkLabel": "default sublink",
"sublinkHref": "#"
}
});
var MainCollection = Backbone.Collection.extend({
model: MainModel
});
var SubCollection = Backbone.Collection.extend({
model: SubModel
});
var mainCollection = new MainCollection(navi, {parse : true});
console.log(mainCollection);
So if you want access the sublinks collection inside each model . You would just use
this.subCollection
which would give you the collection in question.