繁体   English   中英

DOJO:覆盖Dijit小部件中的方法

[英]DOJO: Overriding a method in Dijit widget

我需要将一些参数传递给小部件监视方法,以便可以在CallBack Function中使用

当前代码:

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

    });
});

所需代码:

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

在监视功能中需要引入PanelID。 我已经搜索了文档,但似乎我们无法将参数传递给Watch函数。 有什么方法可以覆盖Watch方法并使其接受参数吗?

您可以将回调函数包装在闭包中,以便保留对PanelID变量的本地引用。 假定PanelID并不是要在watch函数的调用之间更改的东西。

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

watch函数的回调函数创建一个闭包,因此您可以将上面的代码简化为:

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

暂无
暂无

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

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