Javascript 销毁或删除 Backbone.js 中的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6569704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:20:16  来源:igfitidea点击:

Destroy or remove a view in Backbone.js

javascriptjavascript-eventsbackbone.js

提问by Ad Taylor

I'm currently trying to implement a destroy/remove method for views but I can't get a generic solution to work for all my views.

我目前正在尝试为视图实现销毁/删除方法,但我无法获得适用于所有视图的通用解决方案。

I was hoping there would be an event to attach to the controller, so that when a new request comes through it destroys previous views thenloads the new ones.

我希望有一个事件可以附加到控制器上,这样当一个新请求通过它时,它会破坏以前的视图,然后加载新的视图。

Is there any way to do this without having to build a remove function for each view?

有没有办法做到这一点而不必为每个视图构建一个删除函数?

采纳答案by joshvermaire

Without knowing all the information... You could bind a reset trigger to your model or controller:

在不知道所有信息的情况下……您可以将重置触发器绑定到您的模型或控制器:

this.bind("reset", this.updateView);

and when you want to reset the views, trigger a reset.

当您想要重置视图时,触发重置。

For your callback, do something like:

对于您的回调,请执行以下操作:

updateView: function() {
  view.remove();
  view.render();
};

回答by sdailey

I had to be absolutely sure the view was not just removed from DOM but also completely unbound from events.

我必须绝对确定该视图不仅从 DOM 中删除,而且还完全不受事件的约束。

destroy_view: function() {

    // COMPLETELY UNBIND THE VIEW
    this.undelegateEvents();

    this.$el.removeData().unbind(); 

    // Remove view from DOM
    this.remove();  
    Backbone.View.prototype.remove.call(this);

}

Seemed like overkill to me, but other approaches did not completely do the trick.

对我来说似乎有点矫枉过正,但其他方法并不能完全解决问题。

回答by Bassam Mehanni

I know I am late to the party, but hopefully this will be useful for someone else. If you are using backbone v0.9.9+, you could use, listenToand stopListening

我知道我参加聚会迟到了,但希望这对其他人有用。如果你使用的是主干 v0.9.9+,你可以使用,listenTostopListening

initialize: function () {
    this.listenTo(this.model, 'change', this.render);
    this.listenTo(this.model, 'destroy', this.remove);
}

stopListeningis called automatically by remove. You can read more hereand here

stopListening由 自动调用remove。你可以在这里这里阅读更多

回答by JT703

This is what I've been using. Haven't seen any issues.

这是我一直在使用的。没有看到任何问题。

destroy: function(){
  this.remove();
  this.unbind();
}

回答by Dre

According to current Backbone documentation....

根据当前的 Backbone 文档....

view.remove()

视图.remove()

Removes a view and its el from the DOM, and calls stopListening to remove any bound events that the view has listenTo'd.

从 DOM 中移除一个视图及其 el,并调用 stopListening 来移除该视图已经监听的任何绑定事件。

回答by Deot

You could use the way to solve the problem!

你可以用这个方法来解决问题!

initialize:function(){
    this.trigger('remove-compnents-cart');
    var _this = this;
    Backbone.View.prototype.on('remove-compnents-cart',function(){
        //Backbone.View.prototype.remove;
        Backbone.View.prototype.off();
        _this.undelegateEvents();
    })
}

Another way:Create a global variable,like this:_global.routerList

另一种方式:创建一个全局变量,像这样:_global.routerList

initialize:function(){
    this.routerName = 'home';
    _global.routerList.push(this);
}
/*remove it in memory*/
for (var i=0;i<_global.routerList.length;i++){
    Backbone.View.prototype.remove.call(_global.routerList[i]);
}

回答by Chhorn Ponleu

I think this should work

我认为这应该有效

destroyView : function () {
    this.$el.remove();
}