【问题标题】:Straightforward example for loading data into a Sencha GXT (3.0) ListStore using a GWT RPC call?使用 GWT RPC 调用将数据加载到 Sencha GXT (3.0) ListStore 的简单示例?
【发布时间】:2012-05-21 11:18:21
【问题描述】:

是否有人拥有或知道一个示例,该示例演示了使用 Sencha GXT 3.0 通过 GWT RPC 调用将数据加载到 ListStore 中?我知道有很多使用 2.x 版本中使用的 ModelData 和 BeanModel 接口的示例,但是 3.0 不需要使用这些接口,并且据说允许使用实现 ValueProperty 接口的类加载 POJO 对象。

我在 3.0 Explorer 中看到过 RequestFactoryBinding 示例和 RequestFactory Grid 示例,但它们似乎演示了自定义数据代理和接收器的使用。通过查看这些示例中的代码,我假设这些技术/类可能是必需的,但这在任何地方都不明显。可能会有更多文档即将发布,但到目前为止,除了 javadocs 和缺少示例方法中使用的一些源类的 Explorer 之外,我还没有找到任何其他内容。

以下两个示例的链接。

RequestFactoryBinding 示例: http://www.sencha.com/examples/#ExamplePlace:requestfactorybinding

RequestFactory 网格示例: http://www.sencha.com/examples/#ExamplePlace:requestfactorygrid

【问题讨论】:

    标签: gwt extjs rpc gxt


    【解决方案1】:

    DataProxyLoader 主要用于促进 a) 依靠服务器进行过滤/分页/排序,或 b) 在应用程序的各个部分之间重用以访问相同的数据。在客户端只加载一次数据或完成手动存储管理的情况下(如 2.x 中)不需要它们。

    各种商店加载类(ListStoreBindingLoadResultListStoreBinding)在内部演示了如何为 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));
    

    【讨论】:

    • 谢谢,但也许我没有很好地表达我的问题。我熟悉商店的使用以及从中添加/删除项目,但我一直无法找到使用 3.x 对象和接口的加载过程的清晰示例。我还没有找到一个完全使用 3.x 功能通过 RPC 发起数据请求、接收响应并将响应加载到存储中的示例(RPC 调用 -> 响应 -> 加载器 -> 存储 ->小部件)。
    • 抱歉,我认为您列出的示例已经使加载程序的使用非常清楚 - 我将更新答案以解释加载程序的使用。
    【解决方案2】:

    FWIW 我在远程分页和排序网格中添加了 GWTP Dispatch version。这是带有命令模式扭曲的 GWT RPC。

    假设您熟悉网格,您将需要以下实例:

    • RpcProxy
    • PagingLoader
    • LoadResultListStoreBinding

    以及需要调用的方法:

    • PagingLoader.setRemoteSort(true)
    • PagingLoader.addLoadHandler()
    • Grid.setLoader()
    • PagingToolBar.bind()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多