Я думаю, что есть более простое решение, чем ответ @ Derick:
render : function () {
var children = [];
this.collection.forEach( function( model ) {
var rowView = new RowView( { model : model } );
children.push( rowView.render().el );
} );
this.$el.empty().append( children );
return this;
}
http://jsfiddle.net/tXnTk/
Комментарии к коду в ответе @ Derick (мои комментарии отмечены как «[JMM]:»):
render: function(){
// [JMM]: You should already have this.el / this.$el, so there
// [JMM]: should be no need to mess with this.tagName, className,
// [JMM]: id, etc. Just store the child views in an array.
// create in memory element
var $el = $(this.tagName);
// also get the `className`, `id`, `attributes` if you need them
// append everything to the in-memory element
this.collection.each(function(model){
var rowView = new RowView({model: model});
$el.append(rowView.render().el);
});
// [JMM]: Using replaceWith will wipe out event listener
// [JMM]: registrations and other data associated with the
// [JMM]: DOM element. That's why it's necessary to call
// [JMM]: setElement() afterward. Since you already have
// [JMM]: the DOM element and you just want to change the
// [JMM]: content you might as well just empty it out and
// [JMM]: append the new children.
// replace the old view element with the new one, in the DOM
this.$el.replaceWith($el);
// reset the view instance to work with the new $el
this.setElement($el);
}