Monday, 19 August 2013

Backbone.js - how to make sub models from Json data using collection parse

Backbone.js - how to make sub models from Json data using collection parse

When i fetching the data from server, i am getting raw json for my
navigation. my navigation has the main link and sublinks (drop down ) - in
this case how can i make a main model and nest the sub model within
that..? or how can i make 2 different models for main and sub - links and
connect each other..
Here is the json i am getting from server:
[
{
"label": "General",
"link": "#/general",
"subLinks": [
{
"label": "Dashboard",
"link": "#/dashboard"
},
{
"label": "My Task",
"link": "#/mytask"
},
{
"label": "My Documents",
"link": "#/mydocuments"
},
{
"label": "My Templates",
"link": "#/mytemplates"
},
{
"label": "Search",
"link": "#/search"
}
]
},
{
"label": "Repositories",
"link": "#/reposotories",
"subLinks": []
},
{
"label": "SavedSearches",
"link": "#/savedSearches",
"subLinks": []
},
{
"label": "Favourites",
"link": "#/favourites",
"subLinks": []
},
{
"label": "Reports",
"link": "#/reports",
"subLinks": []
},
{
"label": "Preferences",
"link": "#/preferences",
"subLinks": []
}
]
after i receive my json i use the parse method to manipulate the models:
define(["backbone","models/model","collection/baseCollection"], function
(Backbone,model,baseCollection) {
var mainLinkModel = model.extend({
defaults:{
label:"mainLink",
link:"#",
subLinks:{ // is this correct way to make..?
label:"mainLink",
link:"#",
}
}
})
var headerCollection = baseCollection.extend({
url:function(){
return this.path + "edms/navigationLinksTest"
},
model:model,
initialize:function(){
},
parse:function(response){
var mainNavi = [];
_.each(response.navList, function(m){
// i am making new models.. but how to handle
the sublinks?
mainNavi.push(new mainLinkModel(m));
})
}
});
return new headerCollection;
})
But what i doing the way is not giving the proper result. how to handle
this kind of models.. any one help me in this please..?

No comments:

Post a Comment