看起来每当我调用 view.remove() 时,它不仅会从 dom 中删除视图,还会从整个主体中删除
我在创建每个视图后调用 showView
showView : function(view) {
if (this.currentView) {
this.currentView.remove();
this.currentView.unbind();
if (this.currentView.onClose) {
this.currentView.onClose();
}
}
this.currentView = view;
$('body').html(this.currentView.render().el);
}
由于不再有 body 元素,我无法添加另一个视图
Chrome 调试器输出:
$('html')
<html>
<script id="tinyhippos-injected">…</script>
<head>…</head>
</html>
一旦 view.remove() 运行,屏幕就会变白并且不会重新填充 $('body').html(this.currentView.render().el);
编辑:
我将每个视图从 el: $('.body') 更改为 el: '.mainContent' 并在 index.html 文件中添加了一个。
在 app.showView 中,如果它已被删除,我会添加 mainContent div。最好删除一个 div 而不是整个身体。
if($('.mainContent').length == 0)
$('body').append('<div class="mainContent"></div>');
解决方案:
我需要覆盖我的视图删除方法。我不想删除整个 el,只是内容:Recreating a removed view in bone js
Backbone.View.prototype.remove = function() {
this.undelegateEvents();
this.$el.empty();
return this;
};