如何查看dojo主题事件订阅或取消订阅
How to check the dojo topic event is subscribed or unsubscribed
someMethod : function() {
if ( !this._evt ) {
this._evt = topic.subscribe("some-evt", lang.hitch(this, "_someOtherMethod"));
} else {
this._evt.remove();
//Here this just remove the listener but the object this._evt is not null
}
},
这里我只想知道我们怎么才能知道这个class已经订阅了'some-evt'。
我不想在 this._evt.remove();
之后将 this._evt = null;
设置为空
抱歉,dojo/topic
实现通常不提供 published
/subscribed
的 topics
列表,也不提供 published
/subscribed
到该主题。 Dojo 的实现符合这个标准,即没有内置的获取主题列表的机制。请注意,dojo/topic
只有两个函数,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();
}
}
});
});
希望对您有所帮助
someMethod : function() {
if ( !this._evt ) {
this._evt = topic.subscribe("some-evt", lang.hitch(this, "_someOtherMethod"));
} else {
this._evt.remove();
//Here this just remove the listener but the object this._evt is not null
}
},
这里我只想知道我们怎么才能知道这个class已经订阅了'some-evt'。
我不想在 this._evt.remove();
this._evt = null;
设置为空
抱歉,dojo/topic
实现通常不提供 published
/subscribed
的 topics
列表,也不提供 published
/subscribed
到该主题。 Dojo 的实现符合这个标准,即没有内置的获取主题列表的机制。请注意,dojo/topic
只有两个函数,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();
}
}
});
});
希望对您有所帮助