如何销毁这个 Backbone.js View 实例?
var CheckboxView = Backbone.View.extend({
tagName:'div',
template: _.template(item_temp,{}),
events:{
'click .checkoff_friend':'toggleCheckFriend',
},
initialize: function(){
},
render:function(){
},
toggleCheckFriend:function(){
//destroy this View instance.
}
});
var cv = new CheckboxView();
如何销毁实例?当切换被激活时,我希望该视图的实例永远消失。
var CheckboxView = Backbone.View.extend({
tagName:'div',
template: _.template(item_temp,{}),
events:{
'click .checkoff_friend':'toggleCheckFriend',
},
initialize: function(){
},
render:function(){
},
toggleCheckFriend:function(){
//destroy this View instance.
}
});
var cv = new CheckboxView();
How do I destroy the instance? When toggle is activated, I want the instance of that view to dissapear forever.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对类似问题的回答很受欢迎,并且对我很有用。这是我的 destroy_view 函数
(原始问题https://stackoverflow.com/a/11534056/986832)
响应:
我必须绝对确定视图不仅从 DOM 中删除,而且完全不受事件的绑定。
对我来说似乎有点矫枉过正,但其他方法并没有完全达到目的。
My answer for a similar question was received well, and has worked well for me. Here's a look at my destroy_view function
(orig. question https://stackoverflow.com/a/11534056/986832)
Response:
I had to be absolutely sure the view was not just removed from DOM but also completely unbound from events.
Seemed like overkill to me, but other approaches did not completely do the trick.
不要将实例分配给任何变量(我认为没有任何必要,因为主干中的视图是由事件驱动的),并在您的
toggleCheckFriend
方法中删除所有数据和事件,这使得实例可用用于垃圾收集。Do not assign the instance to any variable (I don't see any need to since Views in backbone are driven by events), and in your
toggleCheckFriend
method remove all data and events, which makes the instance available for garbage collection.该视图背后是否有模型?
如果您希望删除模型(从数据库中),可以使用:
this.model.destroy()
之后,您可以通过调用
this 从 DOM 中仅删除视图本身。删除()。文档提到它相当于
$(this.el).remove()
。请注意,上面的“this”指的是视图本身,因此您必须
_.bindAll(this, 'toggleCheckFriend')
Does that View have a model behind it?
If you want the model removed (from the db), you can use:
this.model.destroy()
After that, you can remove just the View itself from the DOM by calling
this.remove()
. Documentation mentions that it is the equivalent of$(this.el).remove()
.Note that the 'this' above refers to the View itself, so you would have to
_.bindAll(this, 'toggleCheckFriend')