Dec
19
|
By default the RestDataSource only supports static url’s. If your backend requires dynamic url’s then you need to subclass RestDataSource and override transformRequest(). In transformRequest() you can set the actionURL property as required by your backend server.
// Following example shows how to append // the entities if to the static update url final RestDataSource restDataSource = new RestDataSource() { @Override public Object transformRequest(DSRequest dsRequest) { if (dsRequest.getOperationType().equals(DSOperationType.UPDATE)) { int id = JSOHelper.getAttributeAsInt( dsRequest.getData(), "id")); dsRequest.setActionURL( getUpdateDataURL()+"/"+id; } return super.transformRequest(dsRequest); } };
Array ( ) 3 Responses to “How do I generate dynamic url’s with SmartGWT and RestDataSource?”
Leave a Reply
You must be logged in to post a comment.
May 12th, 2009 at 5:29 pm
Hello!
I extended the RestDataSource to include all the necessary stuff to it to generate Restful requests, the only thing missing is dynamic url like your example. But I’m stuck in 2 things: my datasource generate XML, and your case is JSON, right? You can put me in the right path to how extract the data from the request? Thanks!
May 12th, 2009 at 6:22 pm
Sorry… I was lazy. After asking, I tried your solution and magically worked, although my datasource use XML… My solution:
@Override
protected Object transformRequest(DSRequest dsRequest) {
for (OperationBinding operationBinding : operationBindings) {
if (dsRequest.getOperationType().equals(operationBinding.getOperationType())) {
String url = operationBinding.getDataURL();
SC.say(“Request: ” + url);
int start = url.indexOf(‘{‘);
int end = url.indexOf(‘}’);
while (start != -1 && end != -1) {
String parametro = url.substring(start + 1, end);
url = url.replace(“{” + parametro + “}”,
JSOHelper.getAttribute(dsRequest.getData(), parametro));
start = url.indexOf(‘{‘);
end = url.indexOf(‘}’);
}
dsRequest.setActionURL(url);
SC.say(“Url transformada: ” + url);
}
}
return super.transformRequest(dsRequest);
}
January 2nd, 2013 at 8:39 pm
Great Job! Until now I thought that it is impossible to create dynamic url’s with SmartGwt and RestDataSource. Thank U 🙂