I'd like to have the following. Imagine you have a homework assignment that has several problems and a grade. Each problem has several parts and a level of difficulty. Each part has an attribute right or wrong.
I'd like to implement that logic through nested models (or really however you can do it, this is just my best guess). So there would be a model 'part' which has an attribute right or wrong. Then there would be a model called problem which has several parts associated with it (a collection? not sure if this is possible) and an attribute difficulty. Then you would have a model homework which would have several problems associated with it and an attribute grade.
My question is:
Is this possible? If so, what is the syntax for your general model creation? What's the syntax for rendering this?
I'm looking for something general like this:
var Homework=Backbone.model.extend({
defaults:{
grade:100,
parts: var parts=Backbone.model.extend({ .... //this definitely seems wrong });
},
});
var Homeworkview=Backbone.view.extend({
initialize: function(){
//create one question with one part
},
render: function(){
//render template for homework grade then call function to render question, which then renders parts},
});
So how do you do this?
There are many ways to do this. Backbone Layout Manager provides a nice, idiomatic way of handling nested models, but for a smaller (or more specialized) application, you may find yourself wanting to roll your own. As the relationship between
Homework
andProblem
seems very similar to that between aProblem
and aPart
, here's one way you might handle the relationships between the former.Start by defining your model and a collection:
Next, the "parent" model will need some way to track a collection of problems. I wrote a little more explanation here if needed, but the general idea looks like this:
Next up, views. The child view can be whatever you want.
The parent view will probably be a little more complicated. Since you have a collection of all
Problem
models stored within theHomework
model, you can create a new view for each one as needed.Hopefully this helps!