Select to view content in your preferred language

Widget Communication Explained

4076
24
09-06-2011 11:45 AM
RobertScheitlin__GISP
MVP Emeritus
All,

  I have answered many threads where people are attempting to communicate between widget. So I thought that I would document in detail this previously undocumented portion of the Flex Viewer. In this PDF I explain what happens behind the scenes and provide code examples of what you need to add to your widgets. I also provide code for launching a widget from another widget so you can call a particular public function in the second widget. I hope this helps many.
Tags (2)
24 Replies
RobertScheitlin__GISP
MVP Emeritus
Jim,

   Which event listener is calling the sharedDataUpdated2 function and are you calling the fetchSharedData in the init function of the dynamic legend widget?

AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdated2);
                fetchSharedData();
0 Kudos
JamesFaron
Occasional Contributor
Robert,

In the Dynamic Legend widget init, I have it as follows:
AppEvent.addListener(AppEvent.DATA_PUBLISH, sharedDataUpdated);
    AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdated2);
    fetchSharedData();


Thanks,
Jim Faron
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Jim,

   Sorry about that if the fetchAllData is going to work than we have to actually send something using the addSharedData instead of null.

var msArr:ArrayCollection = new ArrayCollection();
                        msArr.addItem("export");
addSharedData("Legend_exportImage",msArr);
0 Kudos
JamesFaron
Occasional Contributor
Robert,

That was it! Thanks again for all of your help.

Jim Faron
0 Kudos
JamesKo
New Contributor III
Hi Robert,

This is has been very helpful but I have another related question. How does one access a variable within a widget from the root application, say index.mxml. I want to save a sharedObject based on a value in a widget.

Thanks in advance.

James
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
James,

   I hope that the widget would be loaded already... If that is the case then all you need to do is add these lines to the index.mxml

var id:Number = ViewerContainer.getInstance().widgetManager.getWidgetId("Your Widget");
var bWidget:IBaseWidget = ViewerContainer.getInstance().widgetManager.getWidget(id,true) as IBaseWidget;
x = bWidget.yourPublicVar;
0 Kudos
JamesKo
New Contributor III
Robert,

Thanks for the quick response...I was getting the following error message:
Access of possibly undefined property 'myPublicVar' through a reference with static type com.esri.viewer.IBaseWidget.

BUT I just added these lines of code to get it working:

var vSW:* = bWidget;
var whatever:String = vSW.myPublicVar;

Thanks again.
0 Kudos
by Anonymous User
Not applicable
Robert -

    I found your document on inter-widget communictation very useful.  However, I get an odd result when I use the following code in an attempt to toggle 2 widgets:

var Widgetw:IBaseWidget = ViewerContainer.getInstance().widgetManager.getWidget(2, false) as IBaseWidget;

    which returns null (as expected) if Widget2 isn't yet open, but the problem is that it ends up launching Widget2 despite the 2nd parameter being set to false. Any ideas?

- Bob
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Bob,

    All the openWidgetIfNot parameter does is if the widget is in a minimized state and the openWidgetIfNot is set to also then it does not set the widgets state to maximized/open.
0 Kudos
by Anonymous User
Not applicable
Robert -

    Thank you for your quick response.  Given that the code block provided seems to end up having the desired result of launching the second widget if it's not already open, is there any other means of toggling 2 widgets visibility that you could recommend? Thanks again for any suggestions you might have.

- Bob
0 Kudos