wowjoy-cis-events
v1.0.4
Published
The complete (but tiny) js events solution - An event bus/emitter, simple DOM event API, and incredibly efficient delegated events.
Downloads
5
Readme
The Custom Event Dispatcher provides the ability to communicate inside your application by dispatching events and listening to them.
Install
npm
npm install --save wowjoy-cis-events
yarn
yarn add --save wowjoy-cis-events
example
import { CustomEvent } from "wowjoy-cis-events";
CustomEvent.on("SHOW_NAME", (data) => {
console.log(data.detail);
});
CustomEvent.dispatch("SHOW_NAME", { name: "wowjoy" });
// Remove event listener
CustomEvent.off("SHOW_NAME");
iframe postmessage 通信方案
import { pmEventUtil } from "wowjoy-cis-events";
new pmEventUtil().send("myChannel", data, function (err) {
// console.log(err)
});
new pmEventUtil().listen("myChannel", function (data) {
console.log(data);
});
对于跨 tab 页面通信 建议使用 broadcast-channel,详细见说明文档
Event Bus 事件总线
import { E } from "wowjoy-cis-events";
E.on("my.bus.event", callback);
Emitting a bus event
E.emit("my.bus.event");
// you can also pass arguments through
E.emit("my.bus.event", arg1, arg2);
移除事件
E.off("my.bus.event", callback);
E.off("my.bus.event");