Select to view content in your preferred language

pass window.postMessage data to widgets

3117
4
Jump to solution
01-12-2016 02:52 PM
XiaoqingGe
Deactivated User

Hi Everyone,

I have a jsp page which will send info by the postMessage function.

eg:

     window.postMessage(object, webappbuilderAppURL);

Then I am able to retrieved it by the following lines.

eg:

     window.addEventListener("message", onMessageReceived, false);

     function onMessageReceived(aEvent) {}

but it ONLY works inside the index.html page.

The question I have is, how can I pass the "object' to my widget?

is there any functions like publishData/onReceiveData/fetchDataByName in the widgets from the index.html page?

Thanks!

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
JordanBaumgardner
Frequent Contributor

What about something like:

index.html {

window.MyPostData = {};

window.addEventListener("message", onMessageReceived, false);

     function onMessageReceived(aEvent) {

           window.MyPostData = aEvent.myResultsInterestedIn;

           dojo.topic("myapp/postDataReady");

     }

}

myWidget.js {

     onStartup: function(){

          if (window.MyPostData === {}) {

               dojo.topic.subscribe("myapp/postDataReady", function(){ call_Init });

          }else{

               call_Init()

          }

     }

}

View solution in original post

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Xiaoqing,

   Looks like dojo topic would be a good fit:

dojo/topic — The Dojo Toolkit - Reference Guide

0 Kudos
XiaoqingGe
Deactivated User

Thanks for you message Robert,

dojo/topic is really handy to pass external data to the widgets!

Thanks

Xiaoqing

0 Kudos
JordanBaumgardner
Frequent Contributor

What about something like:

index.html {

window.MyPostData = {};

window.addEventListener("message", onMessageReceived, false);

     function onMessageReceived(aEvent) {

           window.MyPostData = aEvent.myResultsInterestedIn;

           dojo.topic("myapp/postDataReady");

     }

}

myWidget.js {

     onStartup: function(){

          if (window.MyPostData === {}) {

               dojo.topic.subscribe("myapp/postDataReady", function(){ call_Init });

          }else{

               call_Init()

          }

     }

}

0 Kudos
XiaoqingGe
Deactivated User

Thanks for your reply Jordan! The window.MyPostData works like a charm!

And there is no need to use dojo/topic, the following lines work in my WAB app.

index.html {

window.MyPostData = {};

window.addEventListener("message", onMessageReceived, false);

     function onMessageReceived(aEvent) {

          window.MyPostData = aEvent.myResultsInterestedIn;

     }

}

myWidget.js {

     onStartup: function(){

          if (window.MyPostData === ...) {

               call_Init()

          }else{ 

          }

     }

}

Thanks!!

0 Kudos