1 no arrays but collections
Always what you think in Array of Models in Backbone, think in Collection .
Now you need to implement the MyModels collection and save one instance of this instance in your MyModel instance.
// code simplified and not tested MyModel = Backbone.Model.extend({ initialize: function() { this.nestedMyModels: new MyModels(); }, addMyModel: function( model ) { this.nestedMyModels.add( model ); } }); MyModels = Backbone.Collection.extend({ model: MyModel });
2 use Views for rendering
Always what you think in render , think in View .
And it is recommended that if you have Collection and Model , it is better to have a View for each of them. Thus, the collection view will cause the model to be presented in iteration:
// code simplified and not tested MyModelView = Backbone.View.extend({ render: function(){ this.$el.html( model.get("name") ); var view = new MyModelsView({ collection: this.model.nestedMyModels }); this.$el.append( view.render.el ); return this; } }); MyModelsView = Backbone.View.extend({ render: function(){ this.collection.each( function( model ){ var view = new MyModelView({ model: model }); this.$el.append( view.render.el ); }); return this; } });
source share