Backbone.js rendering / creating multiple models in other models

I would like to have the following. Imagine that you have homework that has several problems and a class. Each problem consists of several parts and difficulty level. Each part has a right or wrong attribute.

I would like to implement this logic through nested models (or, indeed, you can do it, this is just my best guess). Thus, there would be a model “part” that has the right or wrong attribute. Then there will be a model, called a problem, which is connected with it in several parts (the collection is "not sure if this is possible") and the complexity of the attribute. Then you will have the homework of the model, which is associated with several problems associated with it, and an attribute class.

My question is:

Is it possible? If so, what is the syntax for creating your general model? What is the syntax for this?

I am looking for something in common:

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?

0
source share
1 answer

. Backbone Layout Manager , ( ) . Homework Problem Problem a Part, .

:

var Problem = Backbone.Model.extend({
  // defaults, constructor, etc.
});

var ProblemCollection = Backbone.Model.extend({
  model: Problem
});

, "" . , , :

var Homework = Backbone.Model.extend({

  defaults:{
    grade:100,
    problems: []
  },

  initialize: function () {
    // initialize a collection of the "Problems" in this Homework
    this.problems = new ProblemCollection(this.get('parts'));
  }
});

, . , .

var ProblemView = Backbone.View.extend({
  tagName: 'li'
  // rendering, initializers, etc.
});

, , . Problem, Homework, .

var HomeworkView = Backbone.View.extend({

  render: function () {

    // create a container for problems
    var $problems = $('<ul class="problem-list"></ul>');

    // create a view for each problem
    this.model.problems.each(function (model) {
      var $problem = $('<li class="problem"></li>'),
          problemView = new ProblemView({
            model: model,
            el: el
          });
      $problems.append($problem);
    });

    this.$el.empty().append($problems);

    return this;
  }
});

, !

+1

All Articles