Announcement

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

    How to obtain a value from DataSource?

    I know the primary key fieldname and the primary key, and I want to know the value of another known fieldname in the DataSource. How do I do this?

    I have tried with fetchData(Criteria criteria, DSCallback callback), (DSCallback code below)

    SC.say(returnValue) shows the right value but when I use getValue() the return string is Empty.



    Code:
    package com.companyweb.client.ui.data;
    
    import com.smartgwt.client.data.DSCallback;
    import com.smartgwt.client.data.DSRequest;
    import com.smartgwt.client.data.DSResponse;
    import com.smartgwt.client.data.Record;
    import com.smartgwt.client.util.SC;
    
    public class CustomDSCallback implements DSCallback{
    
    	private String returnValue = "";
    	private String fieldNameToGet = "";
    	
    	
    	
    	public CustomDSCallback(String fieldNameToGet){
    		this.fieldNameToGet = fieldNameToGet;
    	}
    	
    	@Override
    	public void execute(DSResponse response, Object rawData, DSRequest request) { 
    		Record fetchedData = response.getData()[0];
    		if(fetchedData != null) {
    			returnValue = fetchedData.getAttribute(fieldNameToGet);
    			SC.say(returnValue);		
    }
    		else
    			SC.say("CustomDSCallback failed!");
    	} 
    	
    	
    	public String getValue() {
    		return returnValue;
    	}
    	
    
    }

    #2
    Hi,

    You have to take into account that fetchData is asynchronous. This means that returnValue will be populated only after response will arrive. So following code will return empty string:
    Code:
    CustomDSCallback callback = new CustomDSCallback("myField");
    dataSource.fetchData(criteria, callback);
    SC.say("Current callback value: " + callback.getValue());
    To prove it - assign initial value to returnValue:
    Code:
    private String returnValue = "NO VALUE";
    Execution sequence will be like this:
    a) created callback;
    b) sent request to server;
    c) printing "Current callback value" + callback.getValue()
    ... some time passes ...
    d) response from server received
    e) new value is assigned to returnValue (in execute() method)
    f) printing returnValue (in execute() method)

    Regards,
    Alius

    Comment


      #3
      Thanks

      but how should I do to accomplish what I want? (get one value from a datasource)



      Originally posted by alius
      Hi,

      You have to take into account that fetchData is asynchronous. This means that returnValue will be populated only after response will arrive. So following code will return empty string:
      Code:
      CustomDSCallback callback = new CustomDSCallback("myField");
      dataSource.fetchData(criteria, callback);
      SC.say("Current callback value: " + callback.getValue());
      To prove it - assign initial value to returnValue:
      Code:
      private String returnValue = "NO VALUE";
      Execution sequence will be like this:
      a) created callback;
      b) sent request to server;
      c) printing "Current callback value" + callback.getValue()
      ... some time passes ...
      d) response from server received
      e) new value is assigned to returnValue (in execute() method)
      f) printing returnValue (in execute() method)

      Regards,
      Alius

      Comment


        #4
        You can access that value after response from server received and callback executed (after step e).

        Comment


          #5
          Originally posted by alius
          You can access that value after response from server received and callback executed (after step e).
          I understand that, and I have tried with this code:

          Code:
          private boolean gotResponseFromServer = false;
          
          ...
          
          	@Override
          	public void execute(DSResponse response, Object rawData, DSRequest request) { 
          ...
          		gotResponseFromServer = true;
          ...
          	} 
          
          	public boolean dataHasArrived() {
          		return gotResponseFromServer;
          	}


          Code:
          	
          CustomDSCallback callback = new CustomDSCallback("myField");
          dataSource.fetchData(criteria, callback);
          while(!callback.dataHasArrived()){}
          SC.say("Current callback value: " + callback.getValue());
          But when this code is executed the browser stop working.

          Comment

          Working...
          X