0

I need to pass some arguments to the widget watch method So that it can be used in CallBack Function

Current Code:

require(["dijit/form/NumberTextBox"], function (NumberTextBox) {
    var txt = new NumberTextBox({}, "text10");
    txt.watch("value", function (name, oldValue, value) {

    });
});

Desired code:

require(["dijit/form/NumberTextBox"], function (NumberTextBox) {
    var txt = new NumberTextBox({}, "text10");
    txt.watch("value", function (name, oldValue, value, PanelID) {
        alert(PanelID);
    },PanelID);
});

Need to induct PanelID in watch function. I have searched the docs but it seems that we cannot pass arguments to Watch function. Is there any way to override the Watch method and make it accept arguments?

asim-ishaq
  • 2,190
  • 5
  • 32
  • 55

1 Answers1

1

You can wrap the callback function in a closure so that it keeps a local reference to the PanelID variable. This assumes that PanelID is something that is not meant to change across invocations of the watch function.

require(["dijit/form/NumberTextBox"], function (NumberTextBox) {
    var txt = new NumberTextBox({}, "text10");
    var PanelID = ... // comes from somewhere

    (function (panelId) {
        txt.watch("value", function (name, oldValue, value) {
            alert(panelId);
        });
    })(PanelID);
});

The callback function to the watch function creates a closure, so you can simplify the above code to just:

require(["dijit/form/NumberTextBox"], function (NumberTextBox) {
    var txt = new NumberTextBox({}, "text10");
    var PanelID = ... // comes from somewhere

    txt.watch("value", function (name, oldValue, value) {
        alert(PanelID);
    });
});
Community
  • 1
  • 1
Lucas
  • 8,035
  • 2
  • 32
  • 45