简体   繁体   English

在DOJO确认对话框中动态设置onExecute回调函数

[英]Dynamically Set onExecute callback function in DOJO Confirm Dialog

I have two html buttons in my jsp. 我的jsp中有两个html按钮。 One is Add and another is Remove. 一个是添加,另一个是删除。 Now If ony of the button is clicked then one dojo confirm dialog would be displayed. 现在,如果仅单击按钮的一个,则将显示一个dojo确认对话框。 On click of 'Ok' in the dialog either Add or Remove Functionality will be executed. 在对话框中单击“确定”后,将执行添加或删除功能。 And this Dojo Dialog in present in one of the parent jsp page which will be reused by other jsp pages where Add or Remove functionlity is present. 父jsp页之一中存在此Dojo对话框,该对话框将被存在添加或删除功能的其他jsp页重用。 Depening on the Add/Remove button click the confirm message also needs to be changed. 只需单击“添加/删除”按钮,确认消息也需要更改。 Ex. 防爆。 for Add, the message should be 'Do you want to Add' , for Remove the message would be 'Do you want to Remove'. 对于“添加”,该消息应为“您要添加”,对于“删除”,该消息应为“您要删除”。 I am able to set the message dynamically in the DOJO Cinfirm Dialog but not able to set the onExecute callback function ie. 我能够在DOJO Cinfirm对话框中动态设置消息,但不能设置onExecute回调函数。 when Ok will be clicked in teh Dialog. 当在对话框中单击确定时。 Below id the code. 在id下面的代码。

NB: I am using DOJO 1.10 library 注意:我正在使用DOJO 1.10库

Confirm Dialog Code: 确认对话框代码:

require(["dijit/ConfirmDialog", "dojo/domReady!"], function(ConfirmDialog){
    myDialog = new ConfirmDialog({
        title: "GCLDW - Confirm Dialog",
        content: "",
        style: "width: 300px;",
        onExecute:function(){
            removeCustomer();
        }
    });
});

HTML Button Code: HTML按钮代码:

<button type="button" id="removeCustomerButton"
        onclick="myDialog.set('content', 'Do you want to remove the selected item ?<br><br>');myDialog.show();">
                                <SPAN class=grey><EM><s:message
                                    code="customer.removeCustomer" text="" /></EM>
                                </SPAN>
</button>

<button type="button" id="addCustomerButton"
        onclick="myDialog.set('content', 'Do you want to Add the selected item ?<br><br>');myDialog.show();">
                                <SPAN class=grey><EM><s:message
                                    code="customer.addCustomer" text=""/></EM>
                                </SPAN>
</button>

Now how to set the onExecute call back function depending on the button click, either it would be addCustomer() or removeCustomer() , and whichever page is using this dialog will have their own implementation of these two method. 现在,如何根据单击按钮来设置onExecute回调函数,这将是addCustomer()或removeCustomer(),并且使用此对话框的任何页面都将具有这两种方法的自己的实现。

Just set the onExecute block- in the same way- how you are setting the content. 只需以相同的方式设置onExecute块即可。

Also a suggestion is- put the JS code seperate from HTML . 还建议将JS代码与HTML分开

Here goes the work-around- 解决方法如下:

HTML Code- HTML代码

<button type="button" id="removeCustomerButton">
        <SPAN class=grey><EM>Remove</EM></SPAN>
</button>

<button type="button" id="addCustomerButton">
        <SPAN class=grey><EM>Add</EM></SPAN>
</button>

& the DOJO- 和DOJO-

    require(["dijit/ConfirmDialog", "dojo/domReady!"], 
            function(ConfirmDialog){
                var myDialog = new ConfirmDialog({
                    title: "GCLDW - Confirm Dialog",
                    content: "",
                    style: "width: 300px;"
                });

                dojo.query('#removeCustomerButton').onclick( function() {
                    myDialog.set('content', 'Do you want to remove the selected item ?<br><br>');
                    myDialog.set('onExecute', function(){removeCustomer()} );   // cant call it directly- must wrap within a function
                    myDialog.show();
                });

                dojo.query('#addCustomerButton').onclick( function() {
                    myDialog.set('content', 'Do you want to Add the selected item ?<br><br>');
                    myDialog.set('onExecute', function(){addCustomer()} );  // cant call it directly- must wrap within a function
                    myDialog.show();
                });


            });


    function removeCustomer(){
        console.log("removeCustomer called on execute");
    }

    function addCustomer(){
        console.log("addCustomer called on execute");
    }

In /dojo-release-1.10.4-src/dijit/_DialogMixin.js , the comments state: /dojo-release-1.10.4-src/dijit/_DialogMixin.js中 ,注释状态为:

execute: function(/*Object*/ /*===== formContents =====*/){
// summary:
//      Callback when the user hits the submit button.
//      Override this method to handle Dialog execution.

I implemented a ConfirmDialog like so: 我实现了一个ConfirmDialog,如下所示:

var confirmDialog = new ConfirmDialog({
    title: core.confirm
});
confirmDialog.startup();

function confirmAction(text, callbackFn) {
    confirmDialog.set("execute", callbackFn);
    confirmDialog.set("content", text);
    confirmDialog.show();
}

and called it as follows: 并称其为:

lib.confirmAction(core.areyousure, function () {
    markedForDeletion.forEach(function (node) {
        // Do deletion
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM