简体   繁体   English

Javascript(Dojo):销毁对象时是否删除事件监听器

[英]Javascript (Dojo): Is event-listener removed when object is destroyed

I create a dialog in which there are some buttons with events. 我创建一个对话框,其中有一些带有事件的按钮。 When I destroy the dialog recursive programmatical or by pressing the X are there deleted all the created event-listener (hide, cancel, click1, click2)? 当我以编程方式销毁对话框或按X时,是否删除了所有已创建的事件侦听器(隐藏,取消,click1,click2)?
Because I call this part (here I posted only a simplified version of my code) very often (for different dialogs) and it seems that my code could have some memory leaks I want to eliminate them. 因为我经常(对于不同的对话框)称呼这一部分(在这里我只发布了代码的简化版本),并且看来我的代码可能存在一些内存泄漏,所以我想消除它们。 So please tell me, is it necassary/make sense to remove the event-listener all by myself like eventHide.remove(); 因此,请告诉我,像eventHide.remove();一样自行删除事件侦听器是否必要/有意义 ?

Additional: I tried to use the on (like the eventHide) for the click- and cancel-events but it didn't work. 另外:我尝试将on(如eventHide)用于单击和取消事件,但是没有用。

var myDialog = new Dialog({
    content: 'Testdialog'
});

myDialog.show();

var btn1 = new dijit.form.Button({ label: "Ok" });
var btn2 = new dijit.form.Button({ label: "Help" });

myDialog.containerNode.appendChild(btn1.domNode);
myDialog.containerNode.appendChild(btn2.domNode);

var eventHide = on.once(myDialog, "hide", function(e){
    console.log('hide');
    myDialog.destroyRecursive();
});

dojo.connect(btn1, "onClick", function(){
    console.log('click ok');
    myDialog.destroyRecursive();
});

dojo.connect(btn2, "onClick", function(){
    console.log('click help');
    myDialog.destroyRecursive();
});

dojo.connect(myDialog, "onCancel", function(){
    console.log('cancel');
});

You could use this.own defined in dijit/Destroyable, which is a base of dijit/_WidgetBase and thus most widgets (make sure your custom widget inherit from hit.). 您可以使用dijit / Destroyable中定义的this.own ,它是dijit / _WidgetBase的基础,因此也是大多数小部件的基础(确保自定义小部件继承自hit。)。

dijit/Destroyable is used to track handles of an instance, and then destroy them when the instance is destroyed. dijit / Destroyable用于跟踪实例的句柄,然后在销毁实例时销毁它们。

More infos: http://dojotoolkit.org/reference-guide/1.10/dijit/Destroyable.html 更多信息: http : //dojotoolkit.org/reference-guide/1.10/dijit/Destroyable.html

http://dojotoolkit.org/reference-guide/1.8/dojo/Evented.html http://dojotoolkit.org/reference-guide/1.8/dojo/Evented.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript如何用事件监听器移动变量? - Javascript how to move variables with event-listener? iOS Safari:“ mousemove”上有事件监听器时不会触发“ click”事件 - iOS Safari: 'click' event not triggerd when there is an event-listener on 'mousemove' 在JavaScript中触发单个事件监听器的多个动作(单击) - Triggering multiple actions for single event-listener(on-click) in javascript 如果调用Javascript事件侦听器并且缺少目标元素,会发生什么情况? - What happens if a Javascript event-listener is called and target element is missing? 有没有办法在 Javascript 中获取所有事件侦听器绑定? - Is there a way to get all event-listener bindings in Javascript? 按钮事件侦听器无响应 - Button Event-listener is unresponsive JavaScript:元素移除事件监听器 - JavaScript: on element removed event listener 关于问答游戏中的事件监听器 - About an event-listener inside a quiz game 我将如何将事件侦听器分配给一个 div 以在单击时允许 div 的宽度和高度增长 20% - How would I assign an event-listener to a div that allows the width and height of the div to grow 20% when clicked javascript引用中的对象,但使用事件侦听器时不 - object in javascript reference by this but not when using event listener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM