Javascript 小部件函数的dojo-lang.hitch
我有一个自定义小部件,我很好奇我是否能以特定的方式使用Javascript 小部件函数的dojo-lang.hitch,javascript,scope,dojo,widget,dom-events,Javascript,Scope,Dojo,Widget,Dom Events,我有一个自定义小部件,我很好奇我是否能以特定的方式使用lang.hitch。以下是场景: 假设我有一个自定义小部件,其中包含一个按钮。该按钮需要一个附加到其onClick事件的函数。因此,在我的模板中,我有: <button data-dojo-type="dijit/form/Button" data-dojo-attach-event="onClick : _onButtonClick" /> 我知道我可以从我的模板中删除数据dojo attach事件,并使用dojo.conn
lang.hitch
。以下是场景:
假设我有一个自定义小部件,其中包含一个按钮
。该按钮需要一个附加到其onClick
事件的函数。因此,在我的模板中,我有:
<button data-dojo-type="dijit/form/Button" data-dojo-attach-event="onClick : _onButtonClick" />
我知道我可以从我的模板中删除数据dojo attach事件
,并使用dojo.connect
和lang.hitch
中的连接,但我想知道我是否可以简单地将\u onbutton单击
功能转换为:
_onButtonClick : lang.hitch(this, function(evt) {
//do something here that needs the scope of my widget (this)
})
数据dojo attach事件
自动将此
的范围设为父窗口小部件
我不是100%确定,但我不认为片段中的这个
的上下文
声明([/deps/{
是您想要的。我相信当绑定该函数时,它将是执行declare函数的范围,而不是小部件的实例。我不确定我是否遵循。在您的示例中使用hitch
使用了更多的代码,而此
引用将不正确。我发现如果我有按钮
在我的带有数据dojo attach event
的小部件模板中,当我调试事件函数时,这个代表按钮,而不是我的小部件。@Brian:你找到解决方案了吗?我有一个类似的场景,我想在点击子小部件(比如按钮)时访问小部件对象
_onButtonClick : lang.hitch(this, function(evt) {
//do something here that needs the scope of my widget (this)
})
_onButtonClick : lang.hitch(this, function(evt) {
//do something here that needs the scope of my widget (this)
})
});