Announcement

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

    Dynamic DataSources

    I have been told that I can use Dynamic DataSources via the DynamicDSGenerator interface. I would like to use this because my server will be responding to requests for data where the actual DataSource description will not known until the server processes the request. As I'm new to SmartGWT it would be really helpful if there was a complete client/server example of something similar to this. I could not find anything in the Smart GWT showcase. I previously asked a similar question (http://forums.smartclient.com/showthread.php?t=19229) but the response was insufficient for me to continue my evaluation.

    #2
    The DynamicDSGenerator interface has the API

    Code:
    DataSource getDataSource(java.lang.String id, DSRequest dsRequest)
    The part you're probably missing is how to create the DataSource in your implementation of this method. You need to do this by using one of the static DataSource.fromXML methods. For example you can build a string representation of the XML datasource in your application based on your dynamic requirements and then pass that to DataSource.fromXML(xmlDS) to create an instance of the dynamic datasource.

    Hope this helps.

    Comment


      #3
      Thanks. This is definitely helpful. So this is the server-side showing how to create a dynamic datasource. If possible, can you speak a bit about how this correlates to the client side? Once I create this server side DataSource, how do I communicate that back to the client so that it can assign this DataSource to a databound component like a TreeGrid which will then presumably ask the server for the actual data?

      Comment


        #4
        This DynamicDSGenerator API gets called by the framework when looking up a datasource by ID. This is mentioned in the javadocs of DynamicDSGenerator

        Comment


          #5
          I have been searching example for dynamic datasource for a while. In my case I send query parameters to the php file (REST). The number of field and type in the response is not known.

          What I need is transform this response into datasource, however I could not find any example. If you point any example I really appreciate..

          Comment


            #6
            I am not sure if this is the best approach but at least a solution.

            Code:
            	OperationBinding fetch = new OperationBinding();
            	fetch.setOperationType(DSOperationType.FETCH);
            	fetch.setDataProtocol(DSProtocol.POSTPARAMS);
            		
            	this.setOperationBindings(fetch);
            	this.setDataFormat(DSDataFormat.JSON);
            	  
            	this.setFetchDataURL("php/clilead/clilead.php");  /* Read   Data */
            
            	
                    String str1;
                    DataSourceTextField tmp = null;
                    DataSourceIntegerField tint = null;
                    /*                                                                                  */
                    /* data array have to be defined and completed before this call */
                    /*                                                                                   */
            
                   for(int i=0; i<data.length; i=i+1) {
            	  str1 = data[i].getAttribute("dbfield");  // Database field
                      if (str1.equals("fl_id")) {    // This field is guarantee and primary index
                	  tint = new DataSourceIntegerField(str21, str1, 150);
                	  tint.setPrimaryKey(true);
                	  this.addField(tint); 
                   }
                   else {
                	  tmp = new DataSourceTextField(str1, str1, 150);
                	  this.addField(tmp);
                  }	 
                }
              }
            
            
            /* Her is how I get the data array*/
            
                             RestDataSource dsFields = new dsFields();
            		 Criteria crt = new Criteria();
            		 crt.addCriteria("uname", uname);
            		 dsFields.fetchData(crt,  new DSCallback() {
            			public void execute(DSResponse response, Object rawData,                               
                                                                 DSRequest request) {
            			   data = response.getData();
            			}
            		 });

            Hope it helps to someone.

            Comment

            Working...
            X