2013-05-02 23 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