抱歉,该dojo/topic
实现通常不提供已published
/subscribed
来,既不谁published
/subscribed
到的topics
列表话题。 Dojo的实现符合这个标准,没有内置机制来获取主题列表。请注意,dojo/topic
只有2个功能,publish
和subscribe
。
你应该实现自己的想法,有点像mixin
与函数来订阅topic
,并保持注册的主题名称的轨道,这只是一个想法
的_TopicMixin.js
define(["dojo/topic"], function(topic){
return {
topicsIndex: {},
mySubscribe: function(topicName, listener){
this.topicsIndex[topicName] = topic.subscribe(topicName, listener);
}
myUnsubscribe: function(topicName){
if(this.topicsIndex[topicName]){
this.topicsIndex[topicName].remove();
delete this.topicsIndex[topicName];
}
}
amISubscribed: function(topicName){
return this.topicsIndex[topicName];
}
};
});
你如何使用它
define(["dojo/_base/declare","myApp/_TopicMixin"], function(declare, _TopicMixin){
return declare([_TopicMixin], {
someMethod : function(){
if (!this.amISubscribed("some-evt")) {
this.mySubscribe("some-evt", lang.hitch(this, "_someOtherMethod"));
} else {
this.myUnsubscribe();
}
}
});
});
希望它可以帮助