Communication between widgets

4582
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
RobertScheitlin__GISP
MVP Esteemed Contributor
All,

   Here is a must read for Widget Communication:

http://forums.arcgis.com/threads/38899-Widget-Communication-Explained
0 Kudos
MLowry
by
Occasional Contributor II
For future use by others:
The new way to call a widget from within a widget is the following:


AppEvent.dispatch(AppEvent.WIDGET_RUN, ViewerContainer.getInstance().getWidgetId("WIDGET_NAME"));
0 Kudos
Nehas
by
New Contributor
Hi..

I am very new to flex development and need urgent help.

I am trying to pass the objectid of the clicked point from one widget to another.
For that I am using addshareddata in my source widget.

recAC.addItem(obj.OBJECTID);
addSharedData("HelloWorldWidget", recAC);


In my destination widget I am adding the following two listeners in the creationcomplete
event of the widget.

AppEvent.addListener(AppEvent.DATA_PUBLISH, sharedDataUpdated);
AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdated);


sharedDataUpdated function is given below

private function sharedDataUpdated(event:AppEvent):void
{
var data:Hashtable = event.data as Hashtable;
Alert.show("Length is "+data);
if (data.containsKey("HelloWorldWidget"))
{
  var myAC:ArrayCollection =data.find("HelloWorldWidget") as ArrayCollection;
    
  Alert.show("Length is "+myAC.length);
}
   
}


The problem is that the first Alert is giving data as null.
And the if condition is not returning true. The code is not going inside the if.



Please help me in retrieving the data here. I am stuck in this for days now.

Thanks in advance.
0 Kudos
IvanBespalov
Regular Contributor
Here was similar task.
And here is other way to do it.
0 Kudos
Nehas
by
New Contributor
Here was similar task.
And here is other way to do it.


Thanks for the prompt reply Ivan.. my problem has been solved finally. :)
I have one more question regarding the query tag.


<esri:Query id="query" outFields="
  • " returnGeometry="false" where="OBJECTID={objid}"/>




  • objid is the value that I have  taken from source widget. Now I want to use it in this widget to query some relavant data.
    This code is not working. Is there some other way of passing script parameter value in query's attribute?
    0 Kudos
    IvanBespalov
    Regular Contributor
    in this case objid must be Bindable parameter:
    // in script tag
    [Bindable]
    private var objid:Number;
    // in some function when object id changed
    objid = somevalue;
    // in declaration tag
    <esri:Query id="query" outFields="
  • " returnGeometry="false" where="OBJECTID={objid}"/>

  • or reset where clause each time object id changed:
    // in some function when object id changed
    query.where = StringUtil.substitute("OBJECTID={0}", somevalue);
    // in declaration tag
    <esri:Query id="query" outFields="
  • " returnGeometry="false" />


  • P.S. create new topic or find existing topic using forum search to discuss about query. This discussion is about "Communication between widgets"
    Good luck
    0 Kudos
    indrasena_ReddyKarumuri
    New Contributor
    Hi,
    I am opening a new Widget(Say  for Example Widget 2) on button click on another Widget(Say  for Example Widget 1).
    whenever i make a change in Widget 1, i need to refresh Widget 2. Please help in doing this.
    Currently i am using ArcGIS Version 3.5.
    Thanks,
    Indra.
    0 Kudos
    IvanBespalov
    Regular Contributor
    Indra,

    *
    one way - Using Singleton

    *
    another way - look at the viewer code

    find class AppEvent.as
    public static const REFRESH_LEGEND : String = "refreshLegend";
    it used to refresh legend widget, so LegendWidget has listener attached during initialization
    AppEvent.addListener(AppEvent.REFRESH_LEGEND, legendRefreshHandler, false, 1);

    You can add to AppEvent.as own code
    public static const REFRESH_MY_WIDGET_NR_2:String = "refreshMyWidgetNr2";

    in SecondWidget code initialization add listener function (you can add it in other places too)
    AppEvent.addListener(AppEvent.REFRESH_MY_WIDGET_NR_2, widgetRefreshHandler, false, 1);
    ...
    private function widgetRefreshHandler(event:AppEvent):void
    {
        // right place to refresh

        // read sended data
        // var sendedData:Object = event.data;
    }

    In FirstWidget code
    var sendedData:Object = null;
    AppEvent.dispatch(AppEvent.REFRESH_MY_WIDGET_NR_2, sendedData);


    -------------
    AppEvent class is good helper in ArcGIS FlexViewer application - it has many needed functions, it is extendable ... :)
    Good luck
    0 Kudos
    SaadiaElaarji
    New Contributor
    hello,
    i'm just trying to make communication between two widgets and execute this code  but it doesn't work,




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

    Widget 2

    Code:

    <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>


    someone could help me !!
    i'm using flex 4.6 + Java
    0 Kudos