Communication between widgets

The BaseWidget class supports reading or sending data between widgets. In the constructor of the BaseWidget class, the publishData, dataFetched, and noData events are subscribed. The BaseWidget class also provides the publishData and fetchData methods.

If you want the widget to publish data, call the publishData method. If you want the widget to receive data from another widget, call the fetchData method, and write code to override the onReceiveData and onNoData methods to read data.

All published data is stored in a data store called DataManager. As a result, the widget can receive published data before it is loaded. For example, widget B wants to read data from widget A. Before widget B is loaded, the data published by widget A is stored in DataManager. When widget B is loaded, it calls the fetchData method to fetch the data from DataManager. After widget B is loaded, it receives all of the published data from widget A. In some case, you may want to open another widget by this.openWidgetById method before sending the message to it. The following diagram illustrates this workflow:

Communication between widgets

See WidgtCommunication sample in \client\stemapp\widgets\samplewidgets directory of Web AppBuilder installation.

Note:

Since version 2.1 and up, you can pass feature set between widgets. See Create a feature action in your widget for details.

The method details are described in the following table:

NameParameter

publishData

Publishes the data.

  • data—The data published by the widget.
  • keepHistory—If true, all published data is stored in DataManager, which may cause memory problems. If false, only the latest data is stored in DataManager. The default value is false.

fetchData

Fetches the data.

  • widgetId-The widget’s ID from which you want to fetch the data. If omitted, all data is fetched. You can get the widget ID from app config.json.

fetchDataByName

Fetches the data

  • widgetName—The widget’s name from which you want to fetch data. Although the widget ID may change from one app to another, the widget name is unique. You can use this method to fetch the data. If the same widget is configured more than once in an app, however, these widgets cannot be identified by the widget name.

onReceiveData

Invoked when data is available.

  • widgetName—The name of the widget that publishes data.
  • widgetId—The ID of the widget that publishes data.
  • data—The data.
  • historyData—The history data that widget has published. It may have three values: undefined, true and object.
    • undefined--Data published without history.
    • true--Data published with history. If the widget wants to fetch the history data, call fetchData method.
    • objec--The history data.