我有一个扩展dijit.Dialog的类,但只为我的网站设置默认功能和按钮。单击对话框的取消按钮时,将运行以下代码:
this.actionDialog.destroyRecursive();
this.actionDialog.destroy();
nb this.actionDialog = dijit.Dialog
有时(并非总是)会抛出以下错误:
Uncaught TypeError: Cannot call method 'destroy' of undefined
DialogUnderlay.xd.js:8
导致以下对话框错误显示。我使用的是Google API的1.5。我错过了底衬代码吗?
Ken回答后抛出错误:
exception in animation handler for: onEnd
TypeError: Cannot read property 'style' of null
来自dojo.xd.js:14
。但代码仍能正常运行。
答案 0 :(得分:5)
我仍然不完全确定问题是什么,除了某些原因dijit.DialogUnderlay
代码变得混乱。 FWIW,这在Dojo 1.6中不会发生。
当我在寻找一些可能的解决方案时,我似乎意外地发现避免这个问题可能就像在销毁它之前立即在对话框上调用hide()
一样容易,例如:
this.actionDialog.hide();
this.actionDialog.destroyRecursive();
或者,您可能有兴趣隐藏对话框,然后在隐藏动画完成后将其销毁。
以下是如何在Dojo 1.5及更早版本(测试1.3 +)上执行此操作:
dlg.connect(dlg._fadeOut, 'onEnd', function() {
this.destroyRecursive();
});
dlg.hide();
在1.6中,fadeOut动画不再在实例上公开(授予它,无论如何它在技术上是私有的),但是onHide
现在在动画结束时触发(而在它开始之前触发之前) 。不幸的是,需要使用setTimeout来解决由于调用onHide
的分支中的其他代码而发生的错误,该错误假定实例上仍然存在某些东西,在我们将其销毁之后不会存在(参见{{3 }})。
dlg.connect(dlg, 'onHide', function() {
setTimeout(function() { dlg.destroyRecursive(); }, 0);
});
dlg.hide();
在JSFiddle上查看它:#12436(请参阅http://jsfiddle.net/3MNRu/1/了解问题中的原始错误)
答案 1 :(得分:2)
dialog.hide()方法返回 Deferred ,您的代码可以更像这样的可读性:
var dialog = this.actionDialog;
dialog.hide().then(function(){ dialog.destroyRecursive(); });
小心不要这样做:
this.actionDialog.hide().then(function(){ this.actionDialog.destroyRecursive(); });
在然后 的背景下,此具有另一种含义!
答案 2 :(得分:1)
您只需要调用destroyRecursive()
第二个destroy命令可能导致错误,错误可能导致其他对话框出现问题。
http://dojotoolkit.org/api/1.3/dijit/_Widget/destroyRecursive
<强> destroyRecursive 强>
销毁此小部件及其后代。这是一个通用的“析构函数”函数,所有小部件用户都应调用它来使用小部件干净地丢弃。一旦窗口小部件被销毁,它就会从管理器对象中删除。
答案 3 :(得分:0)
我收到了IE8错误:'this.focusNode.form' is null or not an object
。我发现这是dialog.hide()
返回延迟的结果。我编写了自己的_closeDialog
,消除了IE错误。
_closeDialog : function(cntxt){
cntxt.popup.hide().then(
function(){
cntxt.popup.destroyRecursive(false);
cntxt.popup.destroy(false);
cntxt.destroyRecursive(false);
cntxt.destroy(false);
});
},