Message handler between site and page code

Here is how I have created a message handler between site and page regions. As you read through I think it will make sense, if you’re an intermediate at wix coding:

Create a collection , needs no content. Set to Member Generated Content I call mine “MsgRouter”.

Datasets:

Datasets are used to invoke the .save() and .onBeforeSave(). I don’t save anything to the collection, I just use Save() and onBeforeSave() as event handlers.

Create a Dataset in the header, set to Read& Write. In my code I call it “dashboardDS”
Created a dataset in the body; set to Read&Write. Hover the Dataset icon, right click and select show on all pages . In my code I call it “pageDS”.

Memory variables

Use local memory variables to store the event information. I put two functions in my public folder; Get and Save Memory variables:

export function GetMemoryCm d(keep) {
let memObj = JSON.parse(memory.getItem(global.CMD_KEY));
//console.log(‘in get memory cmd’ + memObj);
if (keep) {} else
memory.removeItem(memObj); <=== note this removes the object from memory
return memObj;
}
export function SaveMemoryCmd (cmd, value) {
let memObj = MEMORYOBJ;
memObj.cmd = cmd;
if (value)
memObj.value = value;
memory.setItem(global.CMD_KEY, JSON.stringify(memObj));
return true;
}
Code for Save() in page or site
In the page or site code you invoke a save() on the Dataset for the site or page;

SaveMemoryCmd (global.LOGON_USER, collectionData.title);
$w(“ #dashboardDS ").save(); <=== or #pageDS

Code for onBeforeSave in page or site

$w.onReady(()
….
//site code receives this event from page code
$w(" #dashboardDS "). onBeforeSave (() => {
let memObj = GetMemoryCmd (); <=== memobject is your memory object from page/site
switch (memObj.cmd) {
case global.CHAINTITLE_CHG: <== whatever
……
break;
case global.LOGON_USER: <=== whatever

break;
}
})
});

Cheers,
Tom