I am building my first real web application using a backbone, and I am struggling with nested resources.
This is a simplified version of the json response I'm working with:
{ "id": 1, "title": "Test Survey", "groups": [ { "id": 1, "title": "Basic Questions", "questions": [ { "id": 1, "title": "Which is your favorite color?" }, { "id": 2, "title": "Do you have any other hobbies?" } ] }, { "id": 2, "title": "Working Questions", "questions": [ { "id": 3, "title": "Do you think working exp is very important?" } ] } ] }
Basically a Survey object that has many groups, each group has many Questions.
I can't seem to find a good way to get all this data in models / collections.
I currently have:
// Models var Question = Backbone.Model.extend({}); var Group = Backbone.Model.extend({}); var Survey = Backbone.Model.extend({ url: surveyURL }); // Collections var GroupsCollection = Backbone.Collection.extend({}); var QuestionsCollection = Backbone.Collection.extend({}); //Views var SurveyView = Backbone.View.extend({ .. }); var GroupsCollectionView = Backbone.View.extend({ .. }); var QuestionsCollectionView = Backbone.View.extent({ .. }); var survey = new Survey({ groups: new GroupsCollection({model: Group}) }); var groupsView = new GroupsCollectionView({collection: survey.get('groups')});
This seems to work for nesting groups in a Survey model, but how do I store questions in a collection and then assign them to each model in a group collection?
As already mentioned, I'm relatively new to the spine, so if I go the wrong way or the best way to do it, please let me know.
Greetings.
Chris hanson
source share