我有一个自定义的小部件,我很好奇是否可以以特定的方式使用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.connect
和lang.hitch
,但我想知道我是否可以简单地将_onButtonClick
函数转换为:
_onButtonClick : lang.hitch(this, function(evt) {
//do something here that needs the scope of my widget (this)
})
1条答案
按热度按时间z2acfund1#
data-dojo-attach-event
自动使this
的作用域成为父小部件。我不是100%肯定,但我认为代码段中
this
的上下文没有声明([/deps/,{我相信当函数被绑定时,它将是声明函数执行的作用域,而不是小部件的示例。