Javascript 如何使用if语句删除和添加视图

Javascript 如何使用if语句删除和添加视图,javascript,backbone.js,Javascript,Backbone.js,我正在尝试删除旧的carView并在单击next按钮后添加下一个 所有内容都来自JSON文件,并且正确递增,但我希望视图也发生变化 以下是我的视图代码: window.CarContainerView = Backbone.View.extend({ el: $('#car-container'), template:_.template($('#tpl-car-container').html()), initialize:function () {

我正在尝试删除旧的
carView
并在单击
next
按钮后添加下一个

所有内容都来自JSON文件,并且正确递增,但我希望视图也发生变化

以下是我的视图代码:

window.CarContainerView = Backbone.View.extend({
    el: $('#car-container'),
    template:_.template($('#tpl-car-container').html()),
    initialize:function () { 
        _.bindAll(this, 'clickNext');
        this.car_number = 0;
        this.car_model = this.model.get('carCollection').models[this.question_number];
        this.question_view = null;
    },
    render:function () {
        $(this.el).html(this.template()); 
        this.car_view = new CarView({el: $(this.el).find('#car'), model: this.car_model});
        this.question_view.render();
        $('#next').bind('click', this.clickNext);        
        return this;
    },
    createNewCar: function () {
        //build
        console.log('createNewCar');
        if(condition) {
            //if the next button is pressed, clear screen and add new screen
        }
    },
    clickNext: function () {
        this.car_number++;
        console.log(this.car_number);
        createNewCar();
    }, 
    clickPrevious: function () { 

    } 
});

评论解释了这些变化。基本上,每次都创建一个新的汽车视图。不要将
el
传递给视图,否则调用该元素时该元素将消失。相反,每次都将新视图渲染到
#car

window.CarContainerView = Backbone.View.extend({
    el: $('#car-container'),
    template:_.template($('#tpl-car-container').html()),
    // use events hash instead of directly using jquery.
    events: {
        'click #next': 'clickNext'
    },
    initialize:function () { 
        // no need to use bindAll since using events hash
        // binds the context of clickNext to this view already.
        this.car_number = 0;
    },
    render:function () {
        // use this.$el instead.
        this.$el.html(this.template()); 
        this.createNewCar();      
        return this;
    },
    createNewCar: function () {
        if(this.car_view){
            // cleanup old view.
            this.car_view.remove();
        }
        // do some bounds checking here, or in clickNext... or both!
        var car_model = this.model.get('carCollection').models[this.car_number];

        // create a new view for the new car.
        this.car_view = new CarView({model: car_model});

        // render into #car instead of passing `el` into the view.
        // let Backbone generate a div for the view, you dont need to 
        // set an `el` in the CarView either.
        this.$('#car').html(this.car_view.render().el);
    },
    clickNext: function () {
        this.car_number++;
        this.createNewCar();
    }, 
    clickPrevious: function () { 

    } 
});

谢谢唯一的问题是我一直收到这个错误:TypeError:“null”不是对象(计算“this.question\u view.render”)谢谢@Paul Hoenecke唯一的问题是我一直在得到这个错误:TypeError:“null”不是一个对象(计算“this.question\u view.render”)@user2238696那么。。。您从未将此问题设置为任何内容。我只是把它放在那里,因为它是在你原来的帖子里。如果你不需要,就把它取下来。更新的答案。this.question\视图应该是this.car\视图。当我简化命名时,这是一个类型错误conventions@user2238696那么还有问题吗?我从答案中删除了问题视图。