想象一下,DataSource字段依赖于另一个DataSource的值。如何使用该DataSource的每个ListGrid自动通知任何更改? 如果可能,无需重绘整个ListGrid,只需重新绘制受影响的记录?
将以下类视为 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.
}
}
我们的可观察 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);
}
}
注意: DataSource模板基于GwtRpcDataSource,可以是found here。
答案 0 :(得分:0)
经过一些搜索和实验,我发现你不能轻易地通知DataSource的每个用户。调用fireCallback
时,数据源会在每个单独的侦听器上调用processResponse
。唉,我无法重新创建功能,但幸运的是我找到了适合这种情况的解决方法:
您需要手动使每个侦听器的缓存无效。对于ListGrid,可以使用invalidateCache
,但使用invalidateDisplayValueCache
进行ComboBoxItem。 E.g:
public class ObserverDataSource extends DataSource {
private Set<MyChangeHandler> handlers = new HashSet<MyChangeHandler>();
public void update() {
for(MyChangeHandler handler : handlers){
handler.onChanged(/* Calls invalidateCache on itself */);
}
}
}
其他可能的解决方案可能是重绘整个ListGrid
,但这更有效。