思路的方向:
实现的loader如下:
import java.util.List;
import com.sencha.gxt.data.shared.loader.DataProxy;
import com.sencha.gxt.data.shared.loader.LoadEvent;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadConfigBean;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoadResultBean;
import com.sencha.gxt.data.shared.loader.PagingLoader;
/**
* RestyGwt的请求代理类.
* 使用方式:
* 1. new 内部的CallBack
* 2. 在CallBack里执行请求
* 3. 请求执行完毕后,在MethodCallback里调用RestyGwtPagingLoader的onLoad方法将返回数据传递给表格
* @author JIM
*
* @param <T>
*/
public class RestyGwtPagingLoader<T> extends PagingLoader<PagingLoadConfig, PagingLoadResult<T>>{
public RestyGwtPagingLoader(){
super(null);
}
public RestyGwtPagingLoader(DataProxy<PagingLoadConfig, PagingLoadResult<T>> proxy) {
super(proxy);
}
public interface CallBack<R>{
public void call(int offset, int limit, RestyGwtPagingLoader<R> loader);
}
private CallBack callBack;
public void setCallBack(CallBack callBack) {
this.callBack = callBack;
}
public void onLoad(List<T> list, int totalLength, int offset){
fireEvent(new LoadEvent<PagingLoadConfig, PagingLoadResult<T>>(new PagingLoadConfigBean(), new PagingLoadResultBean<T>(list, totalLength, offset)));//for test
}
@Override
public void load(int offset, int limit) {
setOffset(offset);
setLimit(limit);
callBack.call(offset, limit, this);
}
@Override
protected void loadData(final PagingLoadConfig config) {
callBack.call(config.getOffset(), config.getLimit(), this);
}
}
@Override
public Widget asWidget() {
if(widget == null){
store = new ListStore<T>(getModelKeyProvider());
columnModel = new ColumnModel<T>(createColumnConfig());
loader = new RestyGwtPagingLoader<T>();
loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig, T, PagingLoadResult<T>>(store)); //将loader的数据存储到store
loader.setCallBack(getRestReq()); //绑定loader的数据请求方式
widget = bindUi();
pagingToolBar.bind(loader);
}
return widget;
}
/**
* restyGwt请求
* @return
*/
protected abstract CallBack<T> getRestReq();
@Override
protected CallBack<ArticleInf> getRestReq() {
return new CallBack<ArticleInf>(){
@Override
public void call(int offset, int limit,final RestyGwtPagingLoader<ArticleInf> loader) {
articleInfClient.findArticleList(1, offset, limit, new RestCallback<List<ArticleInf>>(){ //restyGwt-RPC
@Override
public void onSuccess(Method method, List<ArticleInf> response) {
loader.onLoad(response, response.size(), 0); //TODO 简单测试,需扩展
}
});
}
};
}