我有一个自定义小部件,我很好奇是否可以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从我的模板中删除并使用dojo.connectwith lang.hitchin postCreate,但我想知道我是否可以简单地将_onButtonClick函数转换为:
_onButtonClick : lang.hitch(this, function(evt) {
//do something here that needs the scope of my widget (this)
})