Javascript Backbonejs将2个视图附加到父容器

Javascript Backbonejs将2个视图附加到父容器,javascript,jquery,html,css,backbone.js,Javascript,Jquery,Html,Css,Backbone.js,我不知道如何正确地解释我的问题 我希望在不使用此.setElement()的情况下,始终将两个视图附加到一个div容器中 父视图: var app = app || {}; app.WidgetsView = Backbone.View.extend({ el: $('.widgets'), initialize: function(options) { self = this; this.collection = options.collection; this.r

我不知道如何正确地解释我的问题

我希望在不使用此.setElement()的情况下,始终将两个视图附加到一个div容器中

父视图:

var app = app || {};

app.WidgetsView = Backbone.View.extend({
el: $('.widgets'),

initialize: function(options) {
    self = this;
    this.collection = options.collection;
    this.render();
},

render: function() {
    this.collection.each(function(item) {
            this.renderViews(item)

    }, this);
},

renderViews: function(item) {
    this.View = new app.WidgetView({model: item});
    this.$el.append(this.View.render().el);
},


});
子视图:

var app = app || {};

app.WidgetView = Backbone.View.extend({
className                               : 'widget six columns',
widgetTemplate                          : $('#widgetTemplate').html(),

initialize: function(options) {
    var self = this;
    this.model = options.model;

},

render: function() {
    var modelIndexOfCollection = this.model.collection.indexOf(this.model);

    if (!indexOfCollection & 1 === 1) {
        $('.widgets').append('<div class="widgets-row"></div>');
        this.setElement($('.widgets-row').last());
    } else {
        this.setElement($('.widgets-row').last());
    }
        this.$el.append(_.template(this.widgetTemplate)(this.model.toJSON()))   

    return this;

},

});
儿童:

var app = app || {};

app.WidgetView = Backbone.View.extend({
className                               : 'widget six columns',
widgetTemplate                          : $('#widgetTemplate').html(),

initialize: function(options) {
    var self = this;
    this.model = options.model;

},

render: function() {
    var indexOfCollection = this.parentEl.collection.indexOf(this.model);
    if (this.isEven(indexOfCollection)) {
        $('.widgets').append('<div class="widgets-row row"></div>');
    }

    ($('.widgets-row').last()).append(this.$el.append(_.template(this.widgetTemplate)(this.model.toJSON())))    


    return this;

},

isEven: function(value) {
    if (value%2 == 0)
        return true;
    else
        return false;
},

});
var-app=app | |{};
app.WidgetView=Backbone.View.extend({
className:“小部件六列”,
widgetTemplate:$('#widgetTemplate').html(),
初始化:函数(选项){
var self=这个;
this.model=options.model;
},
render:function(){
var indexOfCollection=this.parentEl.collection.indexOf(this.model);
如果(此.isEven(indexOfCollection)){
$('.widgets').append('');
}
($('.widgets行').last()).append(this.el.append(u.template(this.widgetTemplate)(this.model.toJSON()))
归还这个;
},
isEven:函数(值){
如果(值%2==0)
返回true;
其他的
返回false;
},
});

已找到解决方案并修改了问题如果您解决了问题,请将您的解决方案作为答案发布(您可以在此基础上回答自己的问题)。这比编辑你的问题要好。
var app = app || {};

app.WidgetsView = Backbone.View.extend({
el: $('.widgets'),

initialize: function(options) {
    self = this;
    this.collection = options.collection;
    this.render();
},

render: function() {
    this.collection.each(function(item) {
            this.renderViews(item)

    }, this);
},

renderViews: function(item) {
    this.View = new app.WidgetView({model: item});
    this.$el.append(this.View.render());
},


});
var app = app || {};

app.WidgetView = Backbone.View.extend({
className                               : 'widget six columns',
widgetTemplate                          : $('#widgetTemplate').html(),

initialize: function(options) {
    var self = this;
    this.model = options.model;

},

render: function() {
    var indexOfCollection = this.parentEl.collection.indexOf(this.model);
    if (this.isEven(indexOfCollection)) {
        $('.widgets').append('<div class="widgets-row row"></div>');
    }

    ($('.widgets-row').last()).append(this.$el.append(_.template(this.widgetTemplate)(this.model.toJSON())))    


    return this;

},

isEven: function(value) {
    if (value%2 == 0)
        return true;
    else
        return false;
},

});