2013-05-02 25 views
0

我有一個自定義小部件,我很好奇,如果我可以使用lang.hitch在一個特定的方式。這裏是場景:道場 - lang.hitch的小工具功能

說我有一個自定義小部件,其中包含一個ButtonButton需要附加到其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.hitchpostCreate刪除data-dojo-attach-event和使用dojo.connect,但我想知道我是否可以簡單地將_onButtonClick函數轉換爲:

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

我不確定我是否遵循在你的例子中使用'hitch'使用更多的代碼,並且這個引用不會是正確的 – 2013-05-03 09:57:22

回答

1

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) 
    }) 

}); 

是你想要什麼。我相信當這個函數被綁定的時候,它將成爲聲明函數在其中執行的範圍,而不是小部件的實例

+0

我發現如果我的widget模板中有一個'Button' 'data-dojo-attach-event',當我調試事件函數'this'代表'Button'而不是我的小部件。 – Brian 2013-05-10 14:49:17

+0

@Brian:你有沒有找到解決辦法?我有一個類似的場景,我想通過單擊子部件來訪問部件對象(比如Button) – 2014-11-06 23:07:27