Hi, I'm trying to code a custom DS for accessing our REST-JSON web services.
I don't really like RestDataSource so here's my own:
My problem is on executeAdd() : now you see a stub json object passed as a string.
What I want to do is to get my new data as json (or simply a string that I can parse) from dsrequest (data come from a listGrid) and use them.
getData() and getDataAsString() keep returning me [Object object]
Can you help me please?
Thanks to all in advice!
F.B.
I don't really like RestDataSource so here's my own:
Code:
package it.celi.client; import it.celi.client.overlay.BusinessFunction; import org.restlet.gwt.Callback; import org.restlet.gwt.Client; import org.restlet.gwt.data.Protocol; import org.restlet.gwt.data.Request; import org.restlet.gwt.data.Response; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JsArray; import com.smartgwt.client.data.DSRequest; import com.smartgwt.client.data.DSResponse; import com.smartgwt.client.data.DataSource; import com.smartgwt.client.data.fields.DataSourceTextField; import com.smartgwt.client.types.DSDataFormat; import com.smartgwt.client.types.DSProtocol; import com.smartgwt.client.widgets.grid.ListGridRecord; public class BusinessFunctionDataSource extends DataSource { private final Client client = new Client(Protocol.HTTP); public BusinessFunctionDataSource() { setDataProtocol(DSProtocol.CLIENTCUSTOM); setDataFormat(DSDataFormat.JSON); setClientOnly(false); DataSourceTextField idField = new DataSourceTextField("id", "ID"); DataSourceTextField descriptionField = new DataSourceTextField("description", "Descrizione"); idField.setPrimaryKey(true); addField(idField); addField(descriptionField); } private final native JsArray<BusinessFunction> asJsonArray(String json) /*-{return eval(json); }-*/; @Override protected Object transformRequest(DSRequest request) { String requestId = request.getRequestId(); DSResponse response = new DSResponse(); response.setAttribute("clientContext", request.getAttributeAsObject("clientContext")); response.setStatus(0); switch (request.getOperationType()) { case FETCH: executeFetch(requestId, request, response); break; case ADD: executeAdd(requestId, request, response); break; case UPDATE: executeUpdate(requestId, request, response); break; case REMOVE: executeRemove(requestId, request, response); break; default: break; } return request.getData(); } protected void executeFetch(final String requestId, DSRequest request, final DSResponse response) { client.get(getDataURL(), new Callback() { @Override public void onEvent(Request request, Response eventResponse) { JsArray<BusinessFunction> jsonBfs = asJsonArray(eventResponse.getEntity().getText()); ListGridRecord[] records = new ListGridRecord[jsonBfs.length()]; for (int i = 0; i < jsonBfs.length(); i++) { records[i] = new ListGridRecord(jsonBfs.get(i)); } response.setData(records); response.setTotalRows(records.length); processResponse(requestId, response); } }); } protected void executeAdd(String requestId, DSRequest request, DSResponse response) { GWT.log("ADD", null); client.post(getDataURL(), "{\"description\":\"scolpita nella pietra\",\"id\":\"DACODICE\"}", new Callback() { @Override public void onEvent(Request request, Response response) { GWT.log("SUCCESS!", null); } }); } protected void executeUpdate(String requestId, DSRequest request, DSResponse response) { GWT.log("UPD" + request.getJsObj().toString(), null); } protected void executeRemove(String requestId, DSRequest request, DSResponse response) { GWT.log("DEL" + request.getData().toString(), null); } }
What I want to do is to get my new data as json (or simply a string that I can parse) from dsrequest (data come from a listGrid) and use them.
getData() and getDataAsString() keep returning me [Object object]
Can you help me please?
Thanks to all in advice!
F.B.