Javascript 带有确认按钮的 Dojo 对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10401512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:10:05  来源:igfitidea点击:

Dojo Dialog with confirmation button

javascriptdojo

提问by techie2k

I would like to add a generic dialog with "Ok" and "Cancel" buttons support callback functions.

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

How can I achieve this with a Dojo AMD?

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

For example:

例如:

  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() }
   }

回答by phusick

Here is the solution I came up when I had been facing the same question. It's not completely programmatic, but using the template makes the code more readable and flexible for changes.

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

Better to see once than to hear 100 times, so see all the code below live at jsFiddle: http://jsfiddle.net/phusick/wkydY/

看一次比听 100 次要好,所以请在 jsFiddle 上查看下面的所有代码:http: //jsfiddle.net/phusick/wkydY/

The main principle I employ is the fact that dijit.Dialog::contentmay not only be a string, but also a widget instance. So I subclass dijit.Dialogto declare ConfirmDialogclass. In ConfirmDialog::constuctor()I declare and instantize a widget from a template and set it to be dialog's content. Then I wire onClickactions in ConfirmDialog::postCreate()method:

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

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");
    }

})

The template markup:

模板标记:

<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>

Now use ConfirmDialoginstead of dijit.Dialog:

现在使用ConfirmDialog代替dijit.Dialog

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

Important:Do not forget to disconnect any connects to the dialogs callbacks and destroy dialog when closed.

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

If you use ConfirmDialogoften and in multiple places of your code consider this:

如果您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;
}

Your code will be more readable and you will not have to deal with cleaning up:

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

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

回答by Ezekiel Baniaga

Here's how to use dijit/ConfirmDialogand configure its buttons.

以下是如何使用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();
});

回答by Erik Anderson

Dojo 1.10 includes a new dijit/ConfirmTooltipDialogwith built-in OK and Cancel buttons.

Dojo 1.10 包含一个新的dijit/ConfirmTooltipDialog,带有内置的 OK 和 Cancel 按钮。

回答by Stefano

Dojo Confirm Dialogis very simple and helpful.
http://dojotoolkit.org/reference-guide/1.10/dijit/ConfirmDialog.html

Dojo Confirm Dialog非常简单且有用。
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.
        }
    });
});