Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Callback handler for RestDataSource

    Calling setUpdateDataURL is a great way to get automatic updates in the background.

    But how do I define a callback handler to do some tweaking of my own when the server responds?

    #2
    Use the transformResponse method

    Here is an example that lets you add custom parameters on the post and lets you set up a listener when the server responds.
    Code:
    package edu.iastate.its.project.model.sharedwidgets.client;
    
    import java.util.HashMap;
    
    import com.smartgwt.client.data.DSRequest;
    import com.smartgwt.client.data.DSResponse;
    import com.smartgwt.client.data.RestDataSource;
    import com.smartgwt.client.types.DSDataFormat;
    import com.smartgwt.client.types.PromptStyle;
    
    import edu.iastate.its.project.model.sharedwidgets.client.common.Util;
    
    public class ProjectDataSource extends RestDataSource {
    	private HashMap<String,String> params = new HashMap<String,String>();
    	private DSListener listener;
    	
    	public ProjectDataSource(String context, String model, String xq) {
    		setDataURL(Util.getDataURL());
    		setDataFormat(DSDataFormat.XML);
    		HashMap<String,String> map = new HashMap<String,String>();
    		map.put("context", context);
    		map.put("model", model);
    		map.put("xq", xq);
    		setDefaultParams(map);
    	}
    	
    	public void addParameter(String name, String value) {
    		params.put(name,value);
    	}
    	
    	public DSListener getListener() {
    		return listener;
    	}
    
    	public void setListener(DSListener listener) {
    		this.listener = listener;
    	}
    
    	protected Object transformRequest(DSRequest request) {
    		request.setParams(params);
    		request.setUseSimpleHttp(true);
    		request.setPromptStyle(PromptStyle.CURSOR);
    		return super.transformRequest(request);
    	}
    	
    	protected void transformResponse(DSResponse response, DSRequest request, Object data) {
    		super.transformResponse(response, request, data);
    		if (listener != null)
    			listener.onData(response,request,data);
    	}
    }

    Comment


      #3
      Thanks for posting that pboysen, that will be useful to a number of users.

      Comment

      Working...
      X