Call functions that are on your site code, from your page code.

If you ever need something like that here is how ( thanks Yoav for the help !!!):


Public Module:

 let callback; 
 
 export function registerCallback(cbk) {   
      callback = cbk; 
 }  
 
 export function yourFunction(data) {   
      callback(data); 
 } 

Site Code:

 import {registerCallback} from 'public/module'; 
 
 $w.onReady(() => {   
      registerCallback(function(data) {     
            // here you get the data
      }); 
 }) 

Page Code:

 import {yourFunction} from 'public/module'; 

 yourFunction(SendTheDataToSiteCode);
3 Likes

Hi Athanasios,

Thank you for your contribution!

I am very new at this. I dropped in the example above to a public module, to site and to page, and I keep getting an error in the public section,

let callback;

export function registerCallback(cbk) {
callback = cbk;
}

export function yourFunction(data) {
callback(data); <----- "TypeError: callback is not a function
}

— Also, do you know of a way to go the other direction: from site code to page code? I have a user event which fires in the site code, and need to send that event notice to an element or a callback function on the page.

Thanks so much in advance!

Tom