How do we access datasources of all layers created by ExB and it uses internally through the webmap added to Data tab of the app?

443
4
Jump to solution
04-04-2025 12:05 PM
Vara_PrasadM_S
Frequent Contributor

Hi Team,

In ExpBuilder, in Data tab, I  have added a webmap and used it as source for map widget. And in our custom widgets, we are trying to access the datasources of every layer (our webmap has group layers, sub layers, feature layers). However, the datasource objects of all layers are undefined. And DatasourceManagerInstance::datasources has only one object which is datasource of WebMap. 

Here is the snippet Im using to retrieve instance of datasource of layers. Always getting the 'Data source object is undefined.'. How can we fix this.

Another point, if we use function 'getOrCreateLayerDataSource' on JimuLayerView, its creating a datasource instance but this instance is different and the datasource instance used by OOTB widgets of ExpB for eg., Select widget is different.

Please guide.

 

		  const allJimuLayerViews = jimuMapView.getAllJimuLayerViews()
  
          // Ensure all layer views are loaded
          jimuMapView.whenAllJimuLayerViewLoaded().then(() => {
            // Use DataSourceManager to get existing data sources
            const dataSourceManager = DataSourceManager.getInstance();
            const dataSourcePromises = allJimuLayerViews.map(jlv => {
              const dataSourceId = jlv.layerDataSourceId; // Assuming layerDataSourceId is available
              if (dataSourceId) {
                return dataSourceManager.getDataSource(dataSourceId);
              } else {
                console.error('Layer DataSource ID not found for layer view:', jlv);
                return Promise.resolve(null); // Return null if dataSourceId is not found
              }
            });
  
            Promise.all(dataSourcePromises).then(dsObjs => {
              dsObjs.forEach(dsObj => {
                if (dsObj) {
                  console.error('Data source object is defined.');
                } else {
                  console.error('Data source object is undefined.');
                }
              });
            }).catch(error => {
              console.error('Error retrieving data sources:', error);
            });
          });

 Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
PartyPelican
Regular Contributor

It might be because the data sources are created on demand. You could use this method to make sure they're created:

jimuLayerView.createLayerDataSource() or

DataSourceManager.getInstance()
.createDataSourceByUseDataSource()

 

You can read about a similar issue I had here

And here is an example of how I used the 2nd method.

View solution in original post

4 Replies
Vara_PrasadM_S
Frequent Contributor

@JeffreyThompson2 , could you please help me in this regards.

0 Kudos
PartyPelican
Regular Contributor

It might be because the data sources are created on demand. You could use this method to make sure they're created:

jimuLayerView.createLayerDataSource() or

DataSourceManager.getInstance()
.createDataSourceByUseDataSource()

 

You can read about a similar issue I had here

And here is an example of how I used the 2nd method.

Vara_PrasadM_S
Frequent Contributor

Thank you @PartyPelican.

I have implemented your suggested code and its giving me DataSource objects. However, would ExB also used same instances for OOTB widgets? Otherwise, widgets will not be in sync. For eg., if we select features of a layer from OOTB select widget and read those selected features from this instance that we create, if they don't match we will not get the selected features. Could you please guide me further. 

Thank you very much.

0 Kudos
Vara_PrasadM_S
Frequent Contributor

Hi @PartyPelican, your sample is working and my code snippet is also working. ExpB is referring to same Datasource object internally. It matched.

Thank you very much.