webmodules / jqbus
基于JavaScript的消息队列
0.0.9
2017-04-21 18:55 UTC
Requires
- webmodules/foo: >=3.0.0
- webmodules/jquery: >=1.8
README
#JQBUS
在我的模块1的某些文件 module1.js 中
var bus = jqbus.instance(); bus.on("when_something_happens", function(){ console.log("I want to be notified, so that i can do something"); });
在我的模块2中,可能是在某个其他文件 module.js 中
var bus = jqbus.instance(); bus.publish("when_something_happens");
现在如您所见,module1 从未与 mmodule2 交互,反之亦然,它们甚至没有被讨论,但两个模块都对 jqbus 有所了解,并且它们创建了各自的实例,并且以某种方式能够进行通信!太好了!
因此,我们从模块2内部触发了模块1的一些函数,即使没有模块1的引用,同样,我们可以在尽可能多的模块中设置尽可能多的监听器。可选地,我们还可以传递数据,并触发回调函数。
bus.on("when_something_happens", function(e,target,data){ //data sent by trigger function console.log("I want to be notified, so that i can do something"); }); bus.publish("when_something_happens", { name : "Hello" });
除了将数据传递给不同的模块外,JQBUS 还能将数据发送到同一域的不同标签页。
我们可以通过简单地使用以下方法来移除监听器:
bus.off();