我有一個自定義小部件,我很好奇,如果我可以使用lang.hitch
在一個特定的方式。這裏是場景:道場 - 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)
}
我知道我可以從我的模板,lang.hitch
在postCreate
刪除data-dojo-attach-event
和使用dojo.connect
,但我想知道我是否可以簡單地將_onButtonClick
函數轉換爲:
_onButtonClick : lang.hitch(this, function(evt) {
//do something here that needs the scope of my widget (this)
})
我不確定我是否遵循在你的例子中使用'hitch'使用更多的代碼,並且這個引用不會是正確的 – 2013-05-03 09:57:22