【问题标题】:Dojo Dialog with confirmation button带有确认按钮的 Dojo 对话框
【发布时间】:2023-04-06 03:17:01
【问题描述】:

我想添加一个带有“确定”和“取消”按钮的通用对话框,支持回调函数。

如何使用 Dojo AMD 实现这一目标?

例如:

  myDialog = new Dialog ({

  title: "My Dialog",
  content: "Are you sure, you want to delete the selected Record?",
  style: "width: 300px",
  onExecute:function(){ //Callback function 
      console.log("Record Deleted") 
  },
  onCancel:function(){ 
      console.log("Event Cancelled") } 
  });
  // create a button to clear the cart
   new Button({ label:"Ok"
       onClick: myDialog.onExecute()
   }

   new Button({ label:"Cancel"
        onClick: function(){ myDialog.onCancel() }
   }

【问题讨论】:

    标签: javascript dojo


    【解决方案1】:

    Dojo 确认对话框 非常简单且有用。
    http://dojotoolkit.org/reference-guide/1.10/dijit/ConfirmDialog.html

    require(["dijit/ConfirmDialog", "dojo/domReady!"], function(ConfirmDialog){
        myDialog = new ConfirmDialog({
            title: "My ConfirmDialog",
            content: "Test content.",
            buttonCancel: "Label of cancel button",
            buttonOk: "Label of OK button",
            style: "width: 300px",
            onCancel: function(){
                //Called when user has pressed the Dialog's cancel button, to notify container.
            },
            onExecute: function(){
               //Called when user has pressed the dialog's OK button, to notify container.
            }
        });
    });
    

    【讨论】:

      【解决方案2】:

      以下是如何使用dijit/ConfirmDialog 并配置其按钮。

      require(["dijit/ConfirmDialog"], function(ConfirmDialog){
      
          // create instance
          var dialog = new ConfirmDialog({
              title: "Session Expiration",
              content: "the test. Your session is about to expire. Do you want to continue?",
              style: "width: 300px"
          });
      
          // change button labels
          dialog.set("buttonOk","Yes");
          dialog.set("buttonCancel","No");
      
          // register events
          dialog.on('execute', function() { /*do something*/ });
          dialog.on('cancel', function() { /*do something*/ });
      
          // show
          dialog.show();
      });
      

      【讨论】:

      • 你也可以在构造函数的option对象中添加onExecute属性,该属性持有事件处理函数,而不是事后注册事件。
      • 这应该是第一答案。
      【解决方案3】:

      Dojo 1.10 包含一个新的dijit/ConfirmTooltipDialog,带有内置的“确定”和“取消”按钮。

      【讨论】:

      【解决方案4】:

      这是我在遇到同样问题时提出的解决方案。它不是完全程序化的,但使用模板使代码更具可读性和更改灵活性。

      看一次总比听 100 次好,所以请在 jsFiddle 现场查看以下所有代码:http://jsfiddle.net/phusick/wkydY/

      我采用的主要原则是dijit.Dialog::content 不仅可以是一个字符串,还可以是一个小部件实例。所以我继承dijit.Dialog 来声明ConfirmDialog 类。在ConfirmDialog::constuctor() 中,我从模板声明并实例化一个小部件,并将其设置为对话框的内容。然后我在ConfirmDialog::postCreate() 方法中连接onClick 动作:

      var ConfirmDialog = declare(Dialog, {
      
          title: "Confirm",
          message: "Are you sure?",
      
          constructor: function(/*Object*/ kwArgs) {
              lang.mixin(this, kwArgs);
      
              var message = this.message;
      
              var contentWidget = new (declare([_Widget, _TemplatedMixin, _WidgetsInTemplateMixin], {
                  templateString: template, //get template via dojo loader or so
                  message: message    
              }));
              contentWidget.startup();
              this.content = contentWidget;
          },
      
          postCreate: function() {
              this.inherited(arguments);
              this.connect(this.content.cancelButton, "onClick", "onCancel");
          }
      
      })
      

      模板标记:

      <div style="width:300px;">
      
        <div class="dijitDialogPaneContentArea">
          <div data-dojo-attach-point="contentNode">
              ${message}              
          </div>
        </div>
      
        <div class="dijitDialogPaneActionBar">
      
          <button
            data-dojo-type="dijit.form.Button"
            data-dojo-attach-point="submitButton"
            type="submit"
          >
            OK
          </button>
      
          <button
            data-dojo-type="dijit.form.Button"
            data-dojo-attach-point="cancelButton"
          >
            Cancel
          </button>
      
        </div>
      
      </div>
      

      现在使用ConfirmDialog 代替dijit.Dialog

      var confirmDialog = new ConfirmDialog({ message: "Your message..."});
      confirmDialog.show();
      

      重要提示:不要忘记断开与对话框回调的任何连接并在关闭时销毁对话框。

      如果您经常在代码的多个位置使用ConfirmDialog,请考虑:

      var MessageBox = {};
      MessageBox.confirm = function(kwArgs) {
          var confirmDialog = new ConfirmDialog(kwArgs);
          confirmDialog.startup();
      
          var deferred = new Deferred();
          var signal, signals = [];
      
          var destroyDialog = function() {
              array.forEach(signals, function(signal) {
                  signal.remove();
              });
              delete signals;
              confirmDialog.destroyRecursive();
          }
      
          signal = aspect.after(confirmDialog, "onExecute", function() {
              destroyDialog();
              deferred.resolve('MessageBox.OK');
          });
          signals.push(signal);
      
          signal = aspect.after(confirmDialog, "onCancel", function() {
              destroyDialog();   
              deferred.reject('MessageBox.Cancel');            
          });
          signals.push(signal);
      
          confirmDialog.show();
          return deferred;
      }
      

      您的代码将更具可读性,并且您不必处理清理工作:

      MessageBox.confirm().then(function() {
          console.log("MessageBox.OK")
      });
      

      【讨论】:

      • 第一个带有内部小部件工作示例的 dojo 小部件我发现工作正常!非常感谢 :)。我发现官方文档在这个主题上很糟糕。
      • 官方文档在大多数情况下都很差;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多