2016-11-24 64 views
1
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 
     } 
    }, 

在这里,我只是想知道如何才能才知道,这个类已经订阅了“一些-EVT”如何检查道场话题事件被订阅或被取消订阅

我不想设定this._evt = null;为空后this._evt.remove();

回答

2

抱歉,该dojo/topic实现通常不提供已published/subscribed来,既不谁published/subscribed到的topics列表话题。 Dojo的实现符合这个标准,没有内置机制来获取主题列表。请注意,dojo/topic只有2个功能,publishsubscribe

你应该实现自己的想法,有点像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(); 
      } 
     } 
    }); 
}); 

希望它可以帮助