dojo - lang.hitch用于窗口小部件函数

fv2wmkja  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(224)

我有一个自定义的小部件,我很好奇是否可以以特定的方式使用lang.hitch
假设我有一个包含Button的自定义小部件,Button需要一个附加到其onClick事件的函数,那么,在我的模板中,我有:

<button data-dojo-type="dijit/form/Button" data-dojo-attach-event="onClick : _onButtonClick" />

然后,在我的小部件.js文件中,我有:

_onButtonClick : function(evt) {
    //do something here that needs the scope of my widget (this)
}

我知道我可以从我的模板中删除data-dojo-attach-event,并在postCreate中使用dojo.connectlang.hitch,但我想知道我是否可以简单地将_onButtonClick函数转换为:

_onButtonClick : lang.hitch(this, function(evt) {
    //do something here that needs the scope of my widget (this)
})
z2acfund

z2acfund1#

data-dojo-attach-event自动使this的作用域成为父小部件。
我不是100%肯定,但我认为代码段中this的上下文没有声明([/deps/,{

_onButtonClick : lang.hitch(this, function(evt) {
      //do something here that needs the scope of my widget (this)
    })

});

我相信当函数被绑定时,它将是声明函数执行的作用域,而不是小部件的示例。

相关问题