[英]Destroy or remove a view in Backbone.js
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 then loads the new ones.我希望有一个事件可以附加到 controller,这样当一个新请求通过时,它会破坏以前的视图,然后加载新的视图。
Is there any way to do this without having to build a remove function for each view?有什么方法可以做到这一点而不必为每个视图构建删除 function ?
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.对我来说似乎有点矫枉过正,但其他方法并没有完全奏效。
Without knowing all the information... You could bind a reset trigger to your model or controller:在不知道所有信息的情况下...您可以将重置触发器绑定到 model 或 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();
};
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, listenTo
and stopListening
如果您使用主干 v0.9.9+,您可以使用、 listenTo
和stopListening
initialize: function () {
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destroy', this.remove);
}
stopListening
is called automatically by remove
. stopListening
由remove
自动调用。 You can read more here and here你可以在这里和这里阅读更多
This is what I've been using.这是我一直在使用的。 Haven't seen any issues.没有看到任何问题。
destroy: function(){
this.remove();
this.unbind();
}
According to current Backbone documentation....根据当前的 Backbone 文档....
view.remove() view.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 以删除视图已监听的任何绑定事件。
I think this should work我认为这应该有效
destroyView : function () {
this.$el.remove();
}
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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.