如何使用if语句删除和添加视图
How to remove and add a view using an if statement using backbone
我试图删除我的旧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 () {
}
});
注释解释了这些变化。基本上,每次都创建一个新的CarView。不要将el
传递给视图,否则当您调用remove
时,该元素将消失。相反,每次都将新视图渲染到#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 () {
}
});
相关文章:
- 如何在不使用ajax的情况下将pair值添加到数组并发送到django中的视图
- 在不刷新整个网格的情况下,向Asp.net网格视图添加新行
- 通过格式化将jquery移动组件动态添加到列表视图中
- 将行从数据库添加到表视图
- 如何将Web视图添加到Appgyver中的视图中
- 将范围添加到角度种子默认视图控制器时,测试套件现在失败
- 完整日历 - 如何在周视图中添加一天中的时间
- 将多个事件动态添加到多个 Web 视图
- 主干.js绑定到集合“添加”呈现视图两次
- 添加视图框时更改鼠标位置
- Django 管理员添加视图弹出对话框
- 正在向网站上的外部链接添加视图计数
- 以编程方式添加视图,然后引用这些视图
- 在Backbone.js中根据条件向不同的el添加视图
- 如何向窗口和视图数组添加视图
- angularjs在解析延迟加载脚本和分别添加视图控制器时会抛出Error: [ng:areq]
- 添加视图打开窗口在钛
- 如何使用if语句删除和添加视图
- 在视图中添加视图只会使android应用程序崩溃
- 用JS添加视图细节按钮