Search code examples
javascriptdialogdojo

Unregistering On-Execute Action on a Confirm Dialog in Dojo


I have a dojo widget as below:

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

On invoking createDialog, a ConfirmDialog gets created. It is initially configured to call Func1 on-execute. The Dialog is then shown.

However, if the user does not do anything for 30 secs, the on-execute action gets changed to call Func2.

My problem is, when the user clicks after 30 secs, instead of just Func2 getting called, both Func1 and Func2 gets called.

How do I un-register so that only Func2 gets called if the execute happens after 30 secs?


Solution

  • From documentation "The return value of on() provides a method that can be used to remove the event listener from the event."

    Ex:

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

    so once function1 is called, you can use the handler (signal) to remove it.