DataProxy 和 Loader 主要用于促进 a) 依靠服务器进行过滤/分页/排序,或 b) 在应用程序的各个部分之间重用以访问相同的数据。在客户端只加载一次数据或完成手动存储管理的情况下(如 2.x 中)不需要它们。
各种商店加载类(ListStoreBinding、LoadResultListStoreBinding)在内部演示了如何为 ListStore 提供项目。第一种方法允许您从 RPC 回调或 RequestFactory 接收器中的 onSuccess 方法替换商店中的现有项目:
List<MyData> newItems = ...;//response from server
ListStore<MyData> store = ...;//current store to replace/update
store.replaceAll(newItems);
如果只加载一次,或者只追加而不替换,则应该使用其他方法:
store.addAll(newItems);
可以使用store.add 逐个添加项目,但是这会导致每个项目发生一个事件,应该避免。
编辑:另外,从 2.x 开始,这可能并不完全清楚,但数据本身不需要超类/接口。 ValueProvider 仅用作如何操作模型的外部抽象 - 如何从任何类型的模型中读取或设置值。 PropertyAccess 接口允许 ValueProvider (和其他)实例仅由属性名称生成,这些值将从使用 bean 访问器获取/设置。加载数据不需要 ValueProvider 类型/实例,仅用于数据小部件本身提取它们正在显示的数据,并在用户编辑值后进行修改。
了解这些部分后,加载器/代理机制将以相同的基本方式加载数据。加载器负责在加载时被告知要使用哪些设置(分页、过滤和/或排序),然后触发加载——不同的子类有不同的职责,接受不同的加载配置类型,并返回不同的结果。然后,DataProxy 是一种机制,它实际上与任何保存数据的对象进行对话,如果在服务器上,则异步进行,并在结果可用时通过回调通知加载器。
问题中列出的示例都使用 RequestFactory,但也有几个使用 RPC 的示例,以及一些仅从 JSON 或 XML 加载的示例。 http://www.sencha.com/examples/#ExamplePlace:paginggrid主要数据加载部分如下:
// The rpc async instance
final ExampleServiceAsync service = GWT.create(ExampleService.class);
// As in Ext GWT 2, there is a convenience proxy for RPC to just pass the callback
// directly to the RPC call. If you need a custom callback, just be sure to invoke
// `callback.onSuccess` with the final result.
RpcProxy<PagingLoadConfig, PagingLoadResult<Post>> proxy = new RpcProxy<PagingLoadConfig, PagingLoadResult<Post>>() {
@Override
public void load(PagingLoadConfig loadConfig, AsyncCallback<PagingLoadResult<Post>> callback) {
service.getPosts(loadConfig, callback);
}
};
// ...
// The loader itself has a reference to the proxy so that loader.load() results
// in a round trip to the server, as outlined above.
final PagingLoader<PagingLoadConfig, PagingLoadResult<Post>> loader = new PagingLoader<PagingLoadConfig, PagingLoadResult<Post>>(
proxy);
loader.setRemoteSort(true);
// This last piece - instead of 2.x where the loader is a parameter to the store,
// in 3 you directly wire the results of the loader to add the items into the
// store, as discussed in the first half of this answer
loader.addLoadHandler(new LoadResultListStoreBinding<PagingLoadConfig, Post, PagingLoadResult<Post>>(store));