[英]Access to widget from topic.subscribe's callback
I'm trying to use Dojo's pub/sub topic module as documented here . 我正在尝试使用Dojo的pub / sub主题模块,如此处所述 。 I am subscribing to an event inside a custom widget's
startup()
method and would like to modify the widget when the topic is triggered. 我正在订阅自定义窗口小部件的
startup()
方法中的事件,并希望在触发主题时修改窗口小部件。
How do I access the widget from the topic.subscribe()
callback? 如何从
topic.subscribe()
回调访问小部件? Inside the callback, this
does not refer to the widget; 在回调内部,
this
并不是指小部件; I'm not sure what it refers to... the this
object includes emit
and on
functions, as well as a declaredClass
field that looks like "uniqName_0"
. 我不确定它是指...
this
对象包括emit
和on
函数,以及一个看起来像"uniqName_0"
的declaredClass
字段。
You want to use dojo/_base/lang::hitch
to change the scope of callback function when subscribing inside the widget method, where this
refers to the widgets instance. 您希望在widget方法中订阅时使用
dojo/_base/lang::hitch
更改回调函数的范围,其中this
引用了小部件实例。 You have a couple of options here: 你有几个选择:
anonymous function: 匿名功能:
topic.subscribe("topic/some", lang.hitch(this, function() { // your callback logic here }));
the method of widget: 小部件的方法:
topic.subscribe("topic/some", lang.hitch(this, "callbackMethod"));
See it in action: http://jsfiddle.net/phusick/N7NGB/ 看到它在行动: http : //jsfiddle.net/phusick/N7NGB/
The widget has some convenience methods that will handle the hitch as phusick describes. 小部件有一些方便的方法,可以处理phusick描述的故障。
In the startup method, you can write 在启动方法中,您可以编写
startup: function() {
this.inherited(arguments);
this.subscribe('topic/some', '_onTopic');
},
_onTopic: function() {
this.something // 'this' is the widget
}
A widget has other convenience methods 小部件有其他便利方法
this.unsubscribe
this.connect
this.disconnect
When using the subscribe
and connect
methods, the unsubscribe
and disconnect
methods will be automatically called when the widget is destroyed. 使用
subscribe
和connect
方法时,将在销毁窗口小部件时自动调用unsubscribe
和disconnect
方法。
http://dojotoolkit.org/api/dijit/_WidgetBase http://dojotoolkit.org/api/dijit/_WidgetBase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.