SmartGWT - 重写 tranformRequest 和 transformResponse
我正在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您自己执行实际请求,因此您首先需要查看
setDataProtocol(DSProtocol.CLIENTCUSTOM);
然后在
onSuccess
和onFailure
中,您需要调用processResponse()
将调用transformResponse()
}
Since you are performing the actual request yourself , you first need to look at
setDataProtocol(DSProtocol.CLIENTCUSTOM);
Then in both
onSuccess
andonFailure
you would need to callprocessResponse()
which will calltransformResponse()
}