【问题标题】:Dojo Exception on hiding a dijit.Dialog隐藏 dijit.Dialog 时出现 Dojo 异常
【发布时间】:2011-04-07 06:51:10
【问题描述】:

我有一个对话框,里面有一个表单。下面的代码只是我想要做的一个例子。当你关闭一个 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 : 对不起我的“法语”英语^^

【问题讨论】:

    标签: dojo dijit.form


    【解决方案1】:

    我会向你介绍你最好的新朋友: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

    【讨论】:

      【解决方案2】:

      嗨,如果我理解正确,您不需要每次都销毁 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();
               }
      

      Example 和里德关于dijit.dialog

      【讨论】:

      • 我知道这种语法,但这不是我想要的。有人可以告诉我我的代码有什么问题吗? PS : dojoType 已弃用,您现在应该使用 data-dojo-type,HTML5 正在增长。
      • 您提供的代码中没有错误。它正在运行 (pastehtml.com/view/1dz0jne.html),可能代码的其他部分有错误...您能否提供更多代码?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多