Communication between widgets

6174
48
09-28-2010 08:07 AM
DonCaviness
New Contributor III
Does anyone know how I would go about communicating between widgets?  I would like to use a button in one widget to call a function in another or change components (label text, visible property, etc) in another widget.  Any help will be appreciated.
Tags (2)
0 Kudos
48 Replies
SandraPanicucci
New Contributor
Used the below code in an attempt to get searchResultAC from the search widget to use the BMPID in an edit widget right now I am getting the shared data at least as far as the sharedDataUpdated function--at least in debugger my BMPID displays as the title under the variables tab but I don't seem to be able to figure out how to display the shared data in an alert box...which isn't my ultimate goal but would get me closer to figuring out how to use the shared data.

Widget 1

 
private function widgetCommunication1():void
{
    addSharedData("your key", shared data(ArrayCollection)); 
}


Widget 2

<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:viewer="com.esri.viewer.*"
                   initialize="basewidget1_initializeHandler(event)"
                   >
    <fx:Script>
        <![CDATA[
            import com.esri.viewer.AppEvent;
           
            import mx.events.FlexEvent;

            protected function basewidget1_initializeHandler(event:FlexEvent):void
            {
                ViewerContainer.addEventListener(AppEvent.DATA_PUBLISH, sharedDataUpdated);
            }
           
            private function sharedDataUpdated(event:AppEvent):void
            {
                var data:Object = event.data;

                if (data.key == "my key") 
                {
                     // do something..     
                }
            }
        ]]>
    </fx:Script>



Hope that helps.
0 Kudos
JesseMetcalfe
New Contributor
Guys stucked here...
My sharedDataUpdated () is not getting called 2nd time when my widget is opened.
See Thread -
0 Kudos
GaneshAnanthakrishnan
New Contributor
Bob,

   You have to know your widgets Id number. The widgets id is not a string it is a sequential number that is assigned when the widget is created in FlexViewer.

Here is the code:

ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, yourWidgetsID#));


I added a new function to the ViewerContainer.mxml for the purpose of finding a widgets id #

//Add this new variable at the beginning with the other  private and public vars
public var _configData:ConfigData;

//Add this new function
                        public function getWidgetId(widgetLabel:String):Number
   {
    var id:Number;
    for (var i:Number = 0; i < _configData.widgets.length; i++)
    {
     if (_configData.widgets.label == widgetLabel)
      id = _configData.widgets.id;
    }
    return id;
   }

//Add this to the postConfigHandler function
_configData = event.data as ConfigData;


Then you can do something like this

ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, ViewerContainer.getWidgetId("Search")));


Hi Robert,

I am trying to open a search results widget from a submit button on my headercontroller widget as opposed to opening it by clicking an icon in the widget menu(the default method). 

Am I correct in my understanding that the above solution will only work if the widget is specified in the config.xml file (which is when the widget id is assigned)?  What if I don't want the widget visible in the menu but I want to open the widget?  How would I reference the widget without the id?
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Ganesh,

   Sounds like you need to go deep into the widgetmanager and look at the onRunWidget method and try and understand how widgets are loaded a individual modules from a url. Good Luck.
0 Kudos
ZhujingXia
Occasional Contributor II
Hi,

Any progress for add in widget from the run time? I'd like to add widget2 from widget1. But the widget2 is did not list in the config.xml file. Anyone sould help on this topic?


Thanks

zhujng
0 Kudos
AnthonyKeogh1
New Contributor III
Bob,

   You have to know your widgets Id number. The widgets id is not a string it is a sequential number that is assigned when the widget is created in FlexViewer.

Here is the code:

ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, yourWidgetsID#));


I added a new function to the ViewerContainer.mxml for the purpose of finding a widgets id #

//Add this new variable at the beginning with the other  private and public vars
public var _configData:ConfigData;

//Add this new function
                        public function getWidgetId(widgetLabel:String):Number
   {
    var id:Number;
    for (var i:Number = 0; i < _configData.widgets.length; i++)
    {
     if (_configData.widgets.label == widgetLabel)
      id = _configData.widgets.id;
    }
    return id;
   }

//Add this to the postConfigHandler function
_configData = event.data as ConfigData;


Then you can do something like this

ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, ViewerContainer.getWidgetId("Search")));




Hi again Robert,    I know this is a fairly old post now but this function would be very useful to me at the minute.
I was wondering how you can call the getWidgetId function the way you are above since the function is not static and there is no instance of ViewerContainer to be able to call the function?
I have added in all of the code above into my ViewerContainer.mxml file and the calls I need into my other file where I want to run the widget but, if I dont make the getWidgetId function static I can't access it and if I do try to make it static I get an "Error #1009: Cannot access a property or method of a null object reference." at line 273: "var wgtContainer:IWidgetContainer = configData.widgetContainers[idx.container].container.obj;" of the WidgetManager.mxml file

Thanks for any help.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Anthony,

All you have to do is call
ViewerContainer.getInstance()
0 Kudos
AnthonyKeogh1
New Contributor III
I tried both
viewerCon:ViewerContainer = ViewerContainer.getInstace();  // inside init()

ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, viewerCon.getWidgetId("Selection") ) );

AND

ViewerContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_RUN, ViewerContainer.getInstance().getWidgetId("Selection") ) );


and I recieved the same error. Im probably making a stupid mistake somewhere.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Anthony,

   Try some simple debuging like:

trace(viewerCon.getWidgetId("Selection"));
or

Alert.show(viewerCon.getWidgetId("Selection"));


Is "Selection" the label that you defined for the widget you are after in the main config.xml?
0 Kudos
AnthonyKeogh1
New Contributor III
Hi Robert, I got it working. Thanks for the help again! Sorry about not replying I have been away the last couple of weeks.
0 Kudos