Reputation: 2909
So I'm using Backbone for my web application and I'm having trouble figuring out how to work with nested models. I have a model based on a java class whos JSON would look something like this:
"AdminSettings":
{
"defaults": {
"deletable":true,
"transferable":false;
"issueLimit":1
}
"displaySettings": {
"tabs":2,
"amounts:[10,20]
}
}
Currently, I have an endpoint set up and a backbone model for the AdminSettings object. I was wondering if there was a specific way to get all the backbone benefits for the objects inside AdminSettings. For example, currently I have to use:
adminSettings.get("defaultValues").shareable
But I want to use:
adminSettings.get("defaultValues").get("shareable")
This isn't the only benefit I'm trying to obtain, just an example.
So yeah, what would be a good way to go about this. I was thinking making a backbone model for each one of the nested objects and setting up endpoints for each one of those, but I'm not completely sure. Anyways, thanks for looking.
Upvotes: 2
Views: 135
Reputation: 13986
You could write your own custom backbone parser:
var DisplaySettingsModel = Backbone.Model.extend({});
var DefaultValuesModel = Backbone.Model.extend({});
var AdminSettingsModel = Backbone.Model.extend({
model: {
defaultValues: DefaultValuesModel
displaySettings: DisplaySettingsModel
},
parse: function(response){
response["defaultValues"] = new DefaultValuesModel(response["defaultValues"], {parse:true});
response["displaySettings"] = new DisplaySettingsModel(response["displaySettings"], {parse:true});
return response;
}
});
Upvotes: 2