JSFiddle - React, Tailwind, and code Playground

by Joseph Chiang

JavaScript

// 跨不同 YUI Instance 溝通的方式
// 利用自定事件 (Custom Event) 是最正規的方式
YUI().use("node", function (Y) {
    // 不同的 Instance 是不會收到的。
    Y.on("custom-event", function () {
        alert("Event Handler for 'custom-event' in 1st instance.");
    });
    // 得用 Global 才收的到,且該事件有設定 broadcast:2 才能被正確訂閱到
    Y.Global.on("custom-event", function (e) {
        alert("Event Handler for 'custom-event' in 1st instance.");
        // 可利用 e.data 取得資料
        // alert(e.data); 
    });
}); 

YUI().use("node", function (Y) {
    Y.publish("custom-event", {
        emitFacade: true,
        broadcast: 2
    });
    // 同樣的 Instance 當然會收到。
    Y.on("custom-event", function (e) {
        alert("Event Handler for 'custom-event' in 2nd instance.");
        // 可利用 e.data 取得資料
        // alert(e.data); 
    });            
    // 隔一秒後觸發一個名為 custom-event 的自定事件
    Y.later(1000, null, function () {        
        Y.fire("custom-event", {data: "123"});        
    });
});