[英]How to emit a custom Event from a custom Class and listen to it in Dojo 1.11?
我正在尝试创建一个自定义 Dojo 类,它发出一个自定义事件,并且我正在尝试从调用类中侦听该事件。
我还没有找到一个完整的样本,我被困在这里。
我尝试了以下方法:
在我的自定义类中,我有以下代码:
on.emit(this, "PointFound", {
Msg:"Found a point Within minDistance",
point:selctedPoint
});
这不会导致任何错误。 我被困在如何听它的地方。
当我尝试时(在正确初始化类之后):
on(findUtilInstance,"PointFound", function(e){
console.log(e);
});
我收到一条错误消息: Error: Target must be an event emitter
当我尝试时(在正确初始化类之后):
findUtilInstance.on("PointFound", function(e){
console.log(e);
});
我收到错误: TypeError: findUtilInstance.on is not a function
Dojo 1.11.2 中触发自定义事件并接收它们的正确方法是什么?
dojo/on
它仅适用于 DOM 节点,如果您从dijit/_WidgetBase
扩展它就可以工作,那么您可以使用一个名为 on() 的方法,例如:
myWidget.on("customevent", function(data) {
console.log( " received notification "+data );
});
如果你从dojo/_base/declare
扩展,你可以考虑使用dojo/topic
并实现发布-订阅模式。 这允许“类/模块”相互通信。
dojo/topic 提供了一个集中式中心,用于按主题发布和订阅全局消息。 可以使用 topic.subscribe() 订阅主题,使用 topic.publish() 可以发布消息。
基本上,您可以将dojo/topic
用于:
require(["dojo/topic"], function(topic){
topic.subscribe("some/topic", function(){
console.log("received:", arguments);
});
// ...
topic.publish("some/topic", "one", "two");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.