Communication between widgets

4131
48
09-28-2010 08:07 AM
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)
Reply
0 Kudos
48 Replies
Regular Contributor
Hi!
This thread helped me to communicate between widgets, I'm sending a graphic from one widget to another which use the graphic as input to an identifyTask. This works fine as long as both widgets are opened.
If the user haven't opened the second widget yet I want to open that widget and send the graphic.
I can open the widget (but I have to hardcode the widget id, the getWidgetId function doesn't work for me, but that's another question I asked in another thread). But if the widget is just opened it obviously didn't had time to add the eventlister and litsen to the AppEvent.DATA_PUBLISH, so nothing happens, the user has to do the same request again.

Any ideas how to accomplish to open a widget and sending data to it at the same time (or at least without any user input after the widget is loaded??
Reply
0 Kudos
MVP Esteemed Contributor
Mattias,

  So are you good on this now based on the other thread?
Reply
0 Kudos
Regular Contributor
Robert,
No I still can't achieve all I want.
If I Have my two widgets open then there is no problem for widget1 to send data to widget2 that uses the data to perform a findtask. But If widgets2 is not loaded it loads but don't perform any findtask. I want it to load and receive the data and perform the findtask.
If widget2 is loaded once and then closed so the state of the widget is closed it works fine, but the problem is when the widget never have been opened/loaded during the session.

I've tried to use some fetchSharedData() code at the creationComplete event for widget2 to see if there is any data with my key in shareddata, but I can't fetch any data, don't know how to use the fetchSharedData.
(I saw another thread about a bug in Datamanager.as, made that change but it didn't changed anything for me).
Reply
0 Kudos
MVP Esteemed Contributor
Mattias,

   In the init of widget 2 you need to add an event listener:

ViewerContainer.addEventListener(AppEvent.DATA_SENT, sharedDataUpdated);

            private function sharedDataUpdated(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:* = frmLocateAddress.getChildAt(0);
      var tAddress:TextInput = fitm.getChildAt(0) as TextInput;
      tAddress.text = obj.paddress;
      showStateAddress();
     }
     dataTable.remove("ParcelAddress");
    }
   }

Obviously the sharedDataUpdated function is specific to something that I am doing but this gives you and idea. Basically my scenario is if someone uses the search widget for an address and the returned results are empty than I will give them and option to launch the locate widget with that same searched address. So when they click yes they want to use locate then I launch the locate widget with the WIDGET_RUN  app event discussed earlier and then because the search widget used the addSharedData function when the widget is launched the code above is used immediately.
Reply
0 Kudos
Regular Contributor
Robert,
I added the event listener to the init of widget2 and the function to that event listener. But still it doesn't work, the function never runs.
My knowledge about AppEvents are limited, but I'm thinking that the event listener is added after the event is dispatched and therefor nothing happens. But if this code work for you, I guess it should work for me to, but it doesn't...

What's the difference between AppEvent.DATA_PUBLISH and AppEvent.DATA_SENT?

The data that is added from addSharedData in widget1 has to be stored somewhere, is there no way to access it from widget2 without any event listeners??
Reply
0 Kudos
MVP Esteemed Contributor
Mattias,

   AppEvent.DATA_PUBLISH is what gets dispatched by the BaseWidget when the addSharedData function is invoked. AppEvent.DATA_SENT is what gets dispatched when the BaseWidget is done adding the data to the hashtable. BTW I forgot that I was calling fetchSharedData(); in my locate widget when it is shown. fetchSharedData is a function that forces the data sent to get dispached.
Reply
0 Kudos
Regular Contributor
Thanks for  all your help Robert!
After I added the call to fetchSharedData() in widget2 it works great.
New Contributor II
Hi,

I am having the same problem, could you explain where and how you've used fetchSharedData()?

Cheers!

Shannaka
Reply
0 Kudos
Regular Contributor
Shannaka,
I guess normally you could use the fetchSharedData() in the init function, I use it a bit later because I call a few other functions in my init functions that creates some components and set there values based on info in the config file. But the important thing is that you've added the eventlistenter ( ViewerContainer.addEventListener(AppEvent.DATA_SENT, sharedDataUpdated); ) before you call fetchSharedData(), it's used just to trigger that event.
Reply
0 Kudos
New Contributor II
Thank you! All working now.
Reply
0 Kudos