Javascript 如何防止主干删除()删除;el";在某种意义上?

Javascript 如何防止主干删除()删除;el";在某种意义上?,javascript,backbone.js,Javascript,Backbone.js,我希望在创建新视图之前删除视图。但是我的要求是view.remove()应该删除视图,而不是删除el元素。说到这里,我不想设置标记名,因为它创建了一个不必要的新元素。是否有任何方法可以从内存中删除视图,从而清除el内容?您可以从抽象视图中重写主干的视图remove方法: remove: function() { // this._removeElement(); this.$el.empty(); this.stopListening(); return this; } 默认源

我希望在创建新视图之前删除视图。但是我的要求是
view.remove()
应该删除视图,而不是删除
el
元素。说到这里,我不想设置
标记名
,因为它创建了一个不必要的新元素。是否有任何方法可以从内存中删除视图,从而清除
el
内容?

您可以从抽象视图中重写主干的视图
remove
方法:

remove: function() {
  // this._removeElement();
  this.$el.empty();
  this.stopListening();
  return this;
}

默认源代码:

我以前用一次性启动器视图解决过这个问题

确保html包含一次性视图的(类或id)锚点:

然后创建一个视图:

var LauncherView = Backbone.View.extend({
    initialize: function(options) {
        this.render();
    },

    render: function() {
        this.$el.html(this.template());
        return this;
    },

    // inner views will be bound to ".launcher-container" via
    // their .el property passed into the options hash.
    template: _.template('<div class="launcher-container"></div>')
});
然后,您可以通过实例化一个新的launcher视图,将其附加到DOM,并通过将其绑定到“.launcher容器”来显示下一个视图,从而创建一个新视图

app.currentLauncherView = new LauncherView({});
$('.content-container').append(app.currentLauncherView.el);
app.throwAway2 = new DisposableView({el: '.launcher-container'});

如果使用此选项会发生什么情况。$el.html(“”)我想在创建新的视图实例之前删除现有的视图实例。
app.currentLauncherView = new LauncherView({});
$('.content-container').append(app.currentLauncherView.el);
app.throwAway2 = new DisposableView({el: '.launcher-container'});