如何在创建后的dojoAMD模块中调用按钮点击事件

how to call the button click event in post create dojo AMD module

本文关键字:调用 按钮 事件 模块 dojoAMD 创建      更新时间:2023-09-26

我在dojo AMD模块中创建了新的dojo应用程序。我需要调用我的按钮在一个脚本文件点击到另一个脚本。但当我点击按钮时,它显示零值错误,我的示例代码如下:

我的功能文件代码是:

define(["dojo/_base/declare"  . . .], // defaultDuration
    function (declare . . ) { 

    var mycode = declare([ContentPane, _WidgetBase, _TemplatedMixin], { 
        toggle: function () {
        //here my function 
        },  
        constructor: function (params /) { 
        },
        postCreate: function () { 
        }
    });
    return mycode;
});

按钮点击事件:

define(["dojo/_base/declare" . . . ],
    function (declare . . .) {
        var evet = declare([dijit._WidgetBase, dijit._TemplatedMixin], { 
            _div: null, 
            constructor: function (div) {
                this._div = div; 
            },
            postCreate: function () { 
                this.inherited(arguments);
                var markbutton = new Button({
                    label: "Mark", 
                }, this.markButtonNode); 
                markbutton.on("click", function (evt) {
                    this._div.toggle(); // error here _div is undefined. 
                });
            }
        });
        return evet;
    });

您需要挂接作用域才能使this工作。

markbutton.on("click", lang.hitch(this, function (evt) {
    this._div.toggle(); 
}));

好吧,您可以将范围存储在变量中,并在函数内部访问该变量。。。。

试试这个:-

          var _this = this;
          markbutton.on("click", function (evt) {
                _this._div.toggle(); 
            });

在这种方法中,不需要"dojo/_base/lang"。。。

希望这对你有帮助。。