Web Appbuilder Communication Between Widgets

8754
6
01-14-2015 02:49 PM
deleted-user-Jie3eyjOl9XM
Occasional Contributor

Has anybody gotten publishData and fetchData to work? I see that all of the included widgets use regular Dojo topic publish/subscribe. So, I wonder if I'm making a mistake trying to use the wab publish and fetch methods.

I have a widget that publishes data. And, I have another widget ready to receive the data. On the receiving widget, in the startup method, I call fetchDataByName, and I get an error. Digging into the wab code, I see that the error occurs in this method of the BaseWidget:

    fetchDataByName: function(widgetName){
      //widgetId, the widget name that you want to read data. it is required.
      var widgets = this.widgetManager.getWidgetsByName(widgetName);


      array.forEach(widgets, function(widget){
        this.fetchData(widget.id);
      }, this);
    }

this.widgetManager is not set.

Does anybody have this working, and have any advice for me?

EDIT:

I was able to get this working using fetchData, rather than fetchDataByName. And, now I see that the widget has to be "open" to subscribe to the events. But, I'd like my widget to be hidden, until it receives data, at which time it should open.

SOLVED:

My problem was that I was trying to make my widget live in a panel. Instead, the widget can be out-of-panel, and I can use regular Dojo to hide the widget. The widget is technically "open", so it receives data. Once the widget receives data, I again use regular Dojo to show some HTML elements.

0 Kudos
6 Replies
StephenLead
Regular Contributor III

Are you able to share a sample application which demonstrates this?

thanks

0 Kudos
MelindaFrost
Occasional Contributor

I ran into same issue with FetchDataByName with this.widgetManager is out of scope for the BaseWidget. I am disappointed to hear both widgets have to be open and I cannot use your workaround for out-of-panel. I was really hoping to use this for following workflow:

widget opens and attempts to fetchDataByName from another app that may or may not be open. if not, do nothing. If second widget does get opened later on and new data is published my first widget receives it from the onReceiveData method.

deleted-user-Jie3eyjOl9XM
Occasional Contributor

I started writing a reply to your post, and it got longer and longer, so I just decided to make a blog post. First one in over a year, so thanks for that push! I extended the publishData and onReceiveData, because I experienced exactly the issues you describe. Here you go:

Get on the “Event Bus” | A.J. Morris

MelindaFrost
Occasional Contributor

Awesome job. I was suspicious it was using topic underneath. My initial workaround was to sadly extent widgetManager to "listen" for my topic.publish and then check on widget load if that widget wanted that info. not what i want to do but getting a demo going to client- fine for now. Another thought i had was looking into the dojo datastore.

0 Kudos
deleted-user-Jie3eyjOl9XM
Occasional Contributor

Initially, I created a hidden "controller" widget that just received message, accessed an api, opened the other widgets, and sent the responses. It's the only way I could centralize logic that would be used by all of the widgets. But, the overhead of the design got to be too much, and I gave up on making that a Widget.

I just added a regular dojo class in the widget folder, which is a singleton, and all of my related widgets load it using regular Dojo AMD. I still use the messaging, though, and the widgets can send message directly to each other. You could mix dojo/topic in there, too. I have a widget that communicates with it's child widgets that way. I suspect that using dojo/topic rather than the publishData is going to save you some headaches.

Funny that you mention dojo/store. I used it to create the select in custom identify. It gave me nothing but trouble, especially on mobile. Not sure why. So I gave up and went back to a regular DOM Select backed by an array.

RobertScheitlin__GISP
MVP Emeritus

AJ,

In the Web AppBuilder 1.1 version there is a an example of widget communication in the client\stemapp\widgets\samplewidgets\WidgetCommunication folder.