[英]How turn off all events before new view rendering
I have several views. 我有几种看法。 In some of them I have the similar events like
在其中一些我有类似的事件
events: {
'click #save': 'save'
}
When I create and render new view old event listening remains so old algorythm still works when I already change the view. 当我创建并渲染新视图时,旧的事件监听仍然存在,因此当我已经更改视图时,旧的算法仍然有效。
As I know there is a stopListening()
function but how can I activate for all previous views. 据我所知,有一个
stopListening()
函数,但是如何激活所有以前的视图。 So when I change the view/page I want disable all previous events. 因此,当我更改视图/页面时,我想禁用所有先前的事件。 How I can do that?
我该怎么做?
ID's are global, you shouldn't have more than one per page. ID是全局的,每页ID不能超过一个。 Append your events to a class instead.
而是将您的事件追加到班级。
events: {
'click .save-btn': 'save'
}
Also, make sure you're disposing your views once you finished using them: 此外,请确保在使用完视图后立即将其释放:
var MyView = Backbone.View.extend({
events: {
'click .save-btn': 'save'
},
...
dispose: function() {
this.unbind();
this.remove();
}
};
var view = MyView();
...
view.dispose();
Cheers. 干杯。
try to use el
. 尝试使用
el
。
var MyView = Backbone.View.extend({
el: '#wrapper_of_save_element',
events: {
'click #save': 'save'
},
save: function() {
...
}
});
so your event is only inside your #wrapper_of_save_element (eg. a wrapper div) 因此您的事件仅在您的#wrapper_of_save_element内部(例如包装器div)
http://backbonejs.org/#View-el http://backbonejs.org/#View-el
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.