3

我有一个对话框,里面有一个表格。下面的代码只是我想要做的一个例子。当您关闭 dijit.Dialog 时,如果您不递归地销毁他的孩子,您就无法重新打开它(使用相同的 id)。

如果您不想破坏您的小部件,您可以执行以下操作:

var createDialog = function(){
    try{
    // try to show the hidden dialog
        var dlg = dijit.byId('yourDialogId');
        dlg.show();
    } catch (err) {
    // create the dialog
        var btnClose = new dijit.form.Button({
           label:'Close',
           onClick: function(){
               dialog.hide();
           }
        }, document.createElement("button"));
        var dialog = new dijit.Dialog({
           id:'yourDialogId',
           title:'yourTitle',
           content:btnClose
        });
        dialog.show();
    }
}

我希望这会有所帮助,但使用此代码引发的错误是:

exception in animation handler for: onEnd (_base/fx.js:153)

Type Error: Cannot call method 'callback' of undefined (_base/fx.js:154)

我不得不说我对这个有点迷茫!它快把我逼疯了^^

PS:对不起我的“法语”英语^^

4

2 回答 2

3

我会把你介绍给你最好的新朋友:dojo.hitch()

这允许您将onClick函数绑定到创建它的上下文。很有可能,当您在代码中按下按钮时,它会将您的.show() .hide()表单称为全局窗口的上下文。 var dlg已绑定到您的createDialog函数,因此全局窗口看不到它的内部,因此全局窗口将其视为undefined.

这是我对您的代码所做的更改的示例:

var createDialog = function(){

    // try to show the hidden dialog
    var dlg = dijit.byId('yourDialogId');
    dlg.show();

    // create the dialog
    var btnClose = new dijit.form.Button({
        label:'Close',
        onClick: function(){
            dojo.hitch(this, dlg.hide());
        }
    }, document.createElement("button"));
    dlg.domNode.appendChild(btnClose.domNode);
    var btnShow = new dijit.form.Button({
        label : 'Open',
        onClick : function() {
            dojo.hitch(this, dlg.show());
        }
    }, document.createElement("Button"));
    dojo.body().appendChild(btnShow.domNode);
};  

dojo.ready(function() {
    createDialog();
}); 

请注意,使用dojo.hitch()将任何未来调用或各种按钮的单击绑定到dlg创建 的上下文,永远授予按钮的onclick方法访问createDialog函数内部的权限(如果var dlg存在)。

于 2011-04-07T23:32:45.047 回答
0

嗨,如果我理解正确,您不需要每次都销毁 dijit.Dialog。例如:

HTML:定义简单按钮:

<button id="buttonTwo" dojotype="dijit.form.Button" onclick="showDialog();" type="button">
   Show me!
</button>

Javascript:

// required 'namespaces'
dojo.require("dijit.form.Button");
dojo.require("dijit.Dialog");

// creating dialog
var secondDlg;
dojo.addOnLoad(function () {
        // define dialog content
        var content = new dijit.form.Button({
                    label: 'close',
                    onClick: function () {
                              dijit.byId('formDialog').hide();
                             }
            });

     // create the dialog:
         secondDlg = new dijit.Dialog({
                id: 'formDialog',
                title: "Programatic Dialog Creation",
                style: "width: 300px",
                content: content
            });
        });

          function showDialog() {
            secondDlg.show();
         }

请参阅有关dijit.dialog 的示例和 reed

于 2011-04-07T07:41:10.250 回答