share data between widgets only works for the first time

5247
13
Jump to solution
12-14-2014 08:52 PM
Ye_FengFeng
New Contributor II

Hi,

I follow the link to share data between widgets : https://community.esri.com/thread/34393

But I can only pass the shared data the first time.

I have Input.widget (widget A) and Records.widget(widget B), and shared object pass from widget A to widget B.

The first time I open widget A and click "OK" button, the data pass to widget B, widget A closed and widget B opened and init() of widget B called.

But the second time when  I open widget A and click "OK" button,widget A closed and widget B opened, but the init() function in widget B not called.

It is because I haven't release widget B when it is reload? How can I solve the issue?

Sorry for the poor English, thank you in advance.

0 Kudos
13 Replies
Ye_FengFeng
New Contributor II

Thank you Robert for the clarification, unfortunately I still cannot get it worked.

I still have something to confirm:

(1) I the widget B init() function, should I use:

AppEvent.addListener(AppEvent.DATA_PUBLISH, sharedDataUpdated);

AppEvent.addListener(AppEvent.DATA_NEW_PUBLISHED, sharedDataUpdated2);

OR use:

AppEvent.addListener(AppEvent.DATA_NEW_PUBLISHED, sharedDataUpdated2); 

AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdated2);

(2) The 2 methods:

private function sharedDataUpdated(event:AppEvent):void 

{

    var data:Object = event.data;                

    if (data.key == "QueryState")

    {

        var theObj:Object = data.collection[0];

        getSqlQuery = theObj.sql;

    }

}

private function sharedDataUpdated2(event:AppEvent):void  // DATA_NEW_PUBLISHED

{

    var dataTable:Hashtable = event.data.data as Hashtable;

    if (dataTable.containsKey("QueryState"))

    {

        var recAC:ArrayCollection = dataTable.find("QueryState") as ArrayCollection;

        var theObj:Object = recAC[0];

        getSqlQuery = theObj.sql;

    }

}

The fact is if I use

        var data:Object = event.data.data;

I can get the shared data.

But if I use

        var dataTable:Hashtable = event.data.data as Hashtable;

I can't get the shared data, in this case cause error.

So sorry for the trouble caused, thanks a lot!

It is so strange that I can add code in the edit view but it is messy after I publish it (there is no tab before the code). Seems I am so stupid.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Ye Feng Feng,

This is what I have in a working Widget B on one of my Flex Viewer 3.6 sites:

Lines one and two are of course in my widgets init function.

In my functions I am finding the key ParcelAddress and then taking this value and entering it into a textbox on my widget B. I then remove that value from the shared data to acknowledge that I have received and used that value passed from widget A.

AppEvent.addListener(AppEvent.DATA_NEW_PUBLISHED, sharedDataUpdated); 
AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdated2);

            private function sharedDataUpdated(event:AppEvent):void
            {
                var dataTable:Hashtable = event.data.data as Hashtable;
                if (dataTable.containsKey("ParcelAddress"))
                {
                    var recAC:ArrayCollection = dataTable.find("ParcelAddress") as ArrayCollection;
                    for (var i:Number = 0; i < recAC.length; i++)
                    {
                        var obj:Object = recAC;
                        var fitm:Object = (useSingleLine)?frmLocateAddress.getChildAt(1):frmLocateAddress.getChildAt(0);
                        var tAddress:Object = fitm.getChildAt(0);
                        tAddress.text = obj.paddress;
                        showStateAddress();
                    }
                    dataTable.remove("ParcelAddress");
                }
            }
            
            private function sharedDataUpdated2(event:AppEvent):void
            {
                var dataTable:Hashtable = event.data as Hashtable;
                if (dataTable.containsKey("ParcelAddress"))
                {
                    var recAC:ArrayCollection = dataTable.find("ParcelAddress") as ArrayCollection;
                    for (var i:Number = 0; i < recAC.length; i++)
                    {
                        var obj:Object = recAC;
                        var fitm:Object = (useSingleLine)?frmLocateAddress.getChildAt(1):frmLocateAddress.getChildAt(0);
                        var tAddress:Object = fitm.getChildAt(0);
                        tAddress.text = obj.paddress;
                        showStateAddress();
                    }
                    dataTable.remove("ParcelAddress");
                }else if(dataTable.containsKey("Deactivate_DrawTool"))
                {
                    setMapAction(null, null, null, null);
                }
            }

So the answer to question 1 is neither it should be:

AppEvent.addListener(AppEvent.DATA_NEW_PUBLISHED, sharedDataUpdated);  
AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdated2);

On statement 2, if you use DATA_NEW_PUBLISHED then you need to use event.data.data as Hashtable and when using  DATA_SENT only use event.data as Hashtable.

For the code being messy in your code inserts make sure that the first line of code that you paste has NO SPACES OR TABS at the very beginning of that line when you paste it in. The once you do the syntax formatting you can add tabs and spaces.

0 Kudos
Ye_FengFeng
New Contributor II

Thank you so much Robert.

Now my related code is:

AppEvent.addListener(AppEvent.DATA_NEW_PUBLISHED,sharedDataUpdated);

AppEvent.addListener(AppEvent.DATA_SENT,sharedDataUpdated2);

fetchSharedData();

     private function sharedDataUpdated(event:AppEvent):void

    {

         var dataTable:Hashtable = event.data.data as Hashtable;

         if (dataTable.containsKey("QueryState"))

         {

            var recAC:ArrayCollection = dataTable.find("QueryState") as ArrayCollection;

            var obj:Object = recAC[0]; 

            getSqlQuery = obj.sql;

            dataTable.remove("QueryState");

         }

     }

     private function sharedDataUpdated2(event:AppEvent):void

     {

          var dataTable:Hashtable = event.data as Hashtable;

         if (dataTable.containsKey("QueryState"))

         { 

            var recAC:ArrayCollection = dataTable.find("QueryState") as ArrayCollection; 

            var obj:Object = recAC[0];

            getSqlQuery = obj.sql;

            dataTable.remove("QueryState");

         }

     }

But the result still the same, only pass the parameter at the first time. Want to cry.

0 Kudos
Ye_FengFeng
New Contributor II

Hello Robert,

Finally realize I am so stupid, I put what I want to reload in init(), which should in sharedDataUpdated and sharedDataUpdated2, thank you so much for the sharing.

0 Kudos