Dojo:删除所有元素侦听器或具有单例侦听器
Dojo: remove all element listeners or having singleton-listener
假设我们每 5 秒刷新一次页面并重建项目列表。重建列表后,我们想为每个项目分配一个事件侦听器:
refreshContent: function() {
array.forEach(items, function(item) {
on(item, 'click', function() {
// do something
});
});
}
问题是一些项目已经有事件侦听器并且重复了。
是否有任何方法可以清除项目的所有先前侦听器或具有 单例 侦听器?
您需要跟踪附加的事件,然后才能分离它们:
refreshContent: function() {
if(handles) {
//clean up old handle
array.forEach(handles, function(handle) {
handle.remove();
});
}
//save all handles in this variable
handles = [];
array.forEach(items, function(item) {
handles.push(on(item, 'click', function() {
// do something
}));
});
}
假设我们每 5 秒刷新一次页面并重建项目列表。重建列表后,我们想为每个项目分配一个事件侦听器:
refreshContent: function() {
array.forEach(items, function(item) {
on(item, 'click', function() {
// do something
});
});
}
问题是一些项目已经有事件侦听器并且重复了。
是否有任何方法可以清除项目的所有先前侦听器或具有 单例 侦听器?
您需要跟踪附加的事件,然后才能分离它们:
refreshContent: function() {
if(handles) {
//clean up old handle
array.forEach(handles, function(handle) {
handle.remove();
});
}
//save all handles in this variable
handles = [];
array.forEach(items, function(item) {
handles.push(on(item, 'click', function() {
// do something
}));
});
}