Announcement

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

    Create DataSource from JavaScriptObject

    Is there a way to instantiate a DataSource from a JavaScriptObject like the following but in SmartGWT...

    Code:
    isc.DataSource.create({
    // json here
    });
    Since there is a constructor for DataSource which takes a JavaScriptObject, I thought all I had to do was pass the object to the constructor and it would build the DataSource with all the fields.

    So I've subclassed RestDataSource and I'm passing a JSONObject to the constructor like so,

    Code:
    public MyDataSource(JSONObject data) {
    	super(data.getJavaScriptObject());
    }
    and my the JSON object looks like this:

    Code:
    {
        ID:"com_test_Customer",
        Constructor:"RestDataSource",
        title: "Customer",
        pluralTitle: "Customers",
        serverType: "generic",
        fields:{
    			middleName:{
    				title: "Middle Name",
    				length: 30,
    				type: "text"
    			},
    			id:{
    				title: "Id",
    				primaryKey: true,
    				required: true,
    				type: "integer"
    			}
        }
    }
    But when I try to use the DataSource it says it isn't created.

    Is it possible to create DataSources this way in SmartGWT? If so, is there a document explaining the format that DataSource's expect the JavaScriptObject to comply with? or an example somewhere?

    Thanks for your help,

    - Greg

    #2
    Try the following :

    Code:
    public MyDataSource(JSONObject data) {
    	super(data.getJavaScriptObject());
            getOrCreateJsObj();
    }
    An explicit register() method will be added to handle such cases in the future.

    Sanjiv

    Comment


      #3
      I'm getting the following exception when I try that, it looks like the getOrCreateJsObj() method is throwing the exception.

      Code:
      Uncaught exception escaped : com.google.gwt.core.client.JavaScriptException
      (TypeError): $wnd.isc[scClassName] is undefined
      Am I specifying the right parameters in the JSON object? I thought Constructor:"RestDataSource" would be specifying the scClassName.

      Comment


        #4
        Try the following :
        Code:
        static class MyDataSource extends RestDataSource {
        		private static native JavaScriptObject createDataSourceFromConfig(JavaScriptObject config) /*-{
        			return $wnd.isc.DataSource.create(config);
        		}-*/;
        		public MyDataSource(JavaScriptObject jsObjConfig) {
        			super(createDataSourceFromConfig(jsObjConfig));
        			this.getOrCreateJsObj();
        		}
        		public MyDataSource(JSONObject jsonConfig) {
        			this(jsonConfig.getJavaScriptObject());
        		}
        	};
        Last edited by pismenko; 15 Jul 2010, 01:54.

        Comment

        Working...
        X