简体   繁体   English

从topic.subscribe的回调访问小部件

[英]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对象包括emiton函数,以及一个看起来像"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: 你有几个选择:

  1. anonymous function: 匿名功能:

     topic.subscribe("topic/some", lang.hitch(this, function() { // your callback logic here })); 
  2. 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. 使用subscribeconnect方法时,将在销毁窗口小部件时自动调用unsubscribedisconnect方法。

http://dojotoolkit.org/api/dijit/_WidgetBase http://dojotoolkit.org/api/dijit/_WidgetBase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以通过在DOJO中一次调用topic.subscribe()来订阅两个不同的主题? - Is it possible to subscribe to two different topics with a single call to topic.subscribe() in DOJO? 从Click回调函数中访问JQuery UI Widget - Access JQuery UI Widget from within Click callback function 如何从javascript订阅JMS主题-需要完整的示例 - How to subscribe to a JMS topic from javascript - need a full example 订阅和取消订阅主题 - firebase - subscribe and unsubscribe to topic - firebase 回调方法中的jQuery Widget Factory访问选项 - jQuery Widget Factory access options in a callback method 是否有Twitter的“关注按钮”小部件的回调选项 - is there a callback option with twitter's “follow button” widget Thingsboard,从小部件的 javascript 访问服务器端属性 - Thingsboard, access a server side attribute from the widget's javascript Ionic 2:通过弹出式回调访问主页的服务 - Ionic 2: access main page's services from popover callback 从回调访问 jwtFromRequest - Access jwtFromRequest from callback AWS IoT:在浏览器中订阅主题 - AWS IoT: Subscribe to Topic in Browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM