Imagine that a DataSource field depends on the values of another DataSource. How would each ListGrid that uses that DataSource be automatically notified of any changes? If possible, without having to redraw the entire ListGrid but only the affected records?
Consider the following class as the observer:
public class ObserverDataSource extends DataSource {
public ObserverDataSource() {
// This field needs to update/notify every ListGrid that uses
// this DataSource when a change occurs in the CountryDataSource.
DataSourceField countryField = new DataSourceTextField("country", "Country");
addField(countryField);
// Other fields...
}
public void update() {
// invalidateCache() doesn't work on its own.
// What will make each object (ListGrid) that uses the DataSource refresh itself?
// Even better if it only refreshes the changed records.
// E.g. now a full redraw of the ListGrid.
}
}
And our observable DataSource:
public class ObservableDataSource extends DataSource {
public ObservableDataSource() {
DataSourceField idField = new DataSourceIntegerField("id", "Id");
idField.setPrimaryKey(true);
DataSourceField countryField = new DataSourceTextField("country", "Country");
DataSourceField codeField = new DataSourceIntegerField("code", "Country code");
setFields(idField, countryField, codeField);
}
public executeFetch(...) {
// Doesn't change anything, don't notify observers.
// Do logic...
processResponse(requestId, response);
}
public executeAdd(...) {
// Changed the data, notify the observer (MyDataSource instance).
// Do logic...
myDataSourceInstance.update();
processResponse(requestId, response);
}
}
Note: the DataSource template is based on GwtRpcDataSource, which can be found here.
After some searching, and experimenting, I found out that you can’t notify every user of the DataSource that easily. A datasource calls
fireCallbackon each separate listener, whenprocessResponsehas been called. Alas, I couldn’t recreate the functionality, but luckily I found a workaround for the situation:You need to invalidate the cache of each listener manually. For ListGrid this can be done with
invalidateCacheand but ComboBoxItem withinvalidateDisplayValueCache. E.g.:Other possible solutions could be to redraw the entire
ListGrid, but this is more effecient.