hxc3-subscriber

subscriber support ie7+

Usage no npm install needed!

<script type="module">
  import hxc3Subscriber from 'https://cdn.skypack.dev/hxc3-subscriber';
</script>

README

hxc3-subscriber

订阅与发布

NPM version build status Test coverage gemnasium deps node version npm download

Install

hxc3-subscriber

Usage

subscribe and publish

// 可以直接引用 dist/subscriber.js 获得 EventTarge
// or 使用 import EventTarge from "hxc3-subscriber";

var a = new EventTarge();


// 发布与订阅
// subscribe
a.on('getTodayData', function(data){ ...  });

// publish
a.trigger('getTodayData', 'hello world');

support one level namespace(支持一层命名空间)

// 支持一层 命名空间
a.on('getTodayData.abc', function(data){ ...  }); 
a.on('getTodayData.cdef', function(data){ ...  }); 

a.trigger('getTodayData', data);

// 不支持多层命名空间 not support 
a.on('getTodayData.cdef.child', function(data){ ...  }); 

解绑方式

// Example 1 - off with a token
var token = EventTarget.on('mytopic', myFunc);
PubSub.off(token);

// Example 2 - off with only namespace
PubSub.off('.anamespace');

// Example 3 - off a topic
PubSub.off('mytopic');

// Example 4 - off a topic with namespace
PubSub.off('mytopic.anamespace');