SmartGWT - 重写 tranformRequest 和 transformResponse

发布于 2024-12-28 10:10:21 字数 1528 浏览 4 评论 0原文

我正在 GWT 中使用 SmartGWT 在 GAE 上编写一个项目。

我有一个带有对象的数据库,每个对象都有一个“父亲”对象和“儿子”,我使用 TreeGrid 来表示它们。我已经有一个 GWT-RPC 服务来获取给定节点的儿子。

我现在需要的是在打开树节点时以某种方式扩展 DataSource 类 st,我将能够使用我自己的服务来获取它的儿子 - 然后将它们作为 TreeGrid 可以使用的东西返回。

我知道我应该重写transformRequest和transformResponse,但我不知道如何。任何代码示例/解释将不胜感激!

这是我到目前为止所得到的 - 不确定它是否正确:

              budgetTree.setDataSource(new DataSource() {

            @Override
            protected Object transformRequest(final DSRequest dsRequest) {

              expensesService.getExpensesByYear(2008,
                  new AsyncCallback<ExpenseRecord[]>() {

                    @Override
                    public void onSuccess(ExpenseRecord[] result) {
                      System.out.println("Returned " + result.length + " expense record ");            
                      dsRequest.setAttribute("dsResult", result);
                    }

                    @Override
                    public void onFailure(Throwable caught) {
                      System.out.println("Failed to run query");
                    }

                  });

              return dsRequest;
            }

            @Override
            protected void transformResponse(DSResponse response, DSRequest request,
                Object data) {    
              Record[] recs = request.getAttributeAsRecordArray("dsResult");
              response.setData(recs);
              super.transformResponse(response, request, data);
            }


          });

I'm writing a project in GWT over GAE with SmartGWT.

I've got a DB with object, each having a "father" object and "sons", and I'm using a TreeGrid to represent them. I already have a GWT-RPC service that gets the sons of a given node.

What I need now is to somehow extend the DataSource class s.t when a tree node is opened, I will be able to use my own service to go and fetch it's sons - and then return them as something the TreeGrid can work with.

I know I'm suppose to override transformRequest and transformResponse, but I have no idea how. Any code sample / explanation will be greatly appreciated!

This is what I have so far - not sure it's even remotely correct:

              budgetTree.setDataSource(new DataSource() {

            @Override
            protected Object transformRequest(final DSRequest dsRequest) {

              expensesService.getExpensesByYear(2008,
                  new AsyncCallback<ExpenseRecord[]>() {

                    @Override
                    public void onSuccess(ExpenseRecord[] result) {
                      System.out.println("Returned " + result.length + " expense record ");            
                      dsRequest.setAttribute("dsResult", result);
                    }

                    @Override
                    public void onFailure(Throwable caught) {
                      System.out.println("Failed to run query");
                    }

                  });

              return dsRequest;
            }

            @Override
            protected void transformResponse(DSResponse response, DSRequest request,
                Object data) {    
              Record[] recs = request.getAttributeAsRecordArray("dsResult");
              response.setData(recs);
              super.transformResponse(response, request, data);
            }


          });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反差帅 2025-01-04 10:10:21

由于您自己执行实际请求,因此您首先需要查看
setDataProtocol(DSProtocol.CLIENTCUSTOM);

然后在 onSuccessonFailure 中,您需要调用 processResponse()将调用 transformResponse()

public class MyDatasource extends DataSource{
    public MyDatasource(){
        setDataProtocol(DSProtocol.CLIENTCUSTOM)           
    }

    @Override
    protected Object transformRequest(final DSRequest dsRequest) {

          expensesService.getExpensesByYear(2008,
              new AsyncCallback<ExpenseRecord[]>() {

                @Override
                public void onSuccess(ExpenseRecord[] result) {
                  DSResponse response = new DSResponse();
                  System.out.println("Returned " + result.length + " expense record ");            
                  dsRequest.setAttribute("dsResult", result);
                  Record[] recs = request.getAttributeAsRecordArray("dsResult");
                  response.setData(recs);
                  processResponse(dsRequest.getRequestId(), dsResponse);
                }

                @Override
                public void onFailure(Throwable caught) {
                  DSResponse response = new DSResponse();
                  System.out.println("Failed to run query");
                  processResponse(dsRequest.getRequestId(), dsResponse);
                }

              });

          return dsRequest;
   }

}

Since you are performing the actual request yourself , you first need to look at
setDataProtocol(DSProtocol.CLIENTCUSTOM);

Then in both onSuccess and onFailure you would need to call processResponse() which will call transformResponse()

public class MyDatasource extends DataSource{
    public MyDatasource(){
        setDataProtocol(DSProtocol.CLIENTCUSTOM)           
    }

    @Override
    protected Object transformRequest(final DSRequest dsRequest) {

          expensesService.getExpensesByYear(2008,
              new AsyncCallback<ExpenseRecord[]>() {

                @Override
                public void onSuccess(ExpenseRecord[] result) {
                  DSResponse response = new DSResponse();
                  System.out.println("Returned " + result.length + " expense record ");            
                  dsRequest.setAttribute("dsResult", result);
                  Record[] recs = request.getAttributeAsRecordArray("dsResult");
                  response.setData(recs);
                  processResponse(dsRequest.getRequestId(), dsResponse);
                }

                @Override
                public void onFailure(Throwable caught) {
                  DSResponse response = new DSResponse();
                  System.out.println("Failed to run query");
                  processResponse(dsRequest.getRequestId(), dsResponse);
                }

              });

          return dsRequest;
   }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文