在Dojo中的“确认”对话框上注销执行中操作

时间:2019-02-13 00:13:41

标签: javascript dialog dojo

我有一个dojo小部件,如下所示:

define("myWidget/AnExperiment", [
    "dojo/_base/declare",
    "dojo/dom-style",
    "dijit/ConfirmDialog",
    "dijit/_WidgetBase"
    ], function(declare, domStyle, ConfirmDialog, _WidgetBase) {

return declare([_WidgetBase], {

myDialog: null,

createDialog: function() {
self = this;

self.myDialog = new ConfirmDialog({
                title: 'MyDialog',
                content: 'This is my Dialog.',
                closable: false
            });

domStyle.set(myDialog.cancelButton.domNode, "display", "none");

self.myDialog.on("execute", function() {
                console.log('Func1 called');
                self.myFunc1();
            });

self.myDialog.show();

setTimeout(function() {
                self.changeDialogAction();
            }, 30000);

}

changeDialogAction: function () {
    var self = this;
    self.myDialog.set("title", 'Changed MyDialog');
    self.myDialog.set("content", 'This is the changed dialog');
    // WHAT DO I DO HERE TO UN-REGISTER OLD ON-EXECUTE ACTION
    self.myDialog.on("execute", function() {
        console.log('Func2 called');
                self.myFunc2();
    });
},

myFunc1: function() {
 console.log('This is func1');
},

myFunc2: function() {
 console.log('This is func2');
}
}   

在调用createDialog时,会创建一个ConfirmDialog。最初将其配置为调用Func1 on-execute。然后显示Dialog

但是,如果用户在30秒内不执行任何操作,则on-execute操作将变为调用Func2

我的问题是,当用户在30秒后单击时,不仅Func2Func1都被调用,而不仅仅是Func2被调用。

如何取消注册,以便在执行执行30秒后仅调用Func2

1 个答案:

答案 0 :(得分:0)

从文档“ on()的返回值提供一种可用于从事件中删除事件侦听器的方法。”

例如:

require(["dojo/on", "dojo/_base/window"], function(on, win){
  var signal = on(win.doc, "click", function(){
    // remove listener after first event
    signal.remove();
    // do something else...
  });
});

因此,一旦调用function1,就可以使用处理程序(信号)将其删除。