com.gwtext.client.data
Class ScriptTagProxy

java.lang.Object
  extended by com.gwtext.client.core.JsObject
      extended by com.gwtext.client.data.DataProxy
          extended by com.gwtext.client.data.ScriptTagProxy

public class ScriptTagProxy
extends DataProxy

An implementation of DataProxy that reads a data object from a URL which may be in a domain other than the originating domain of the running page.

Note that if you are retrieving data from a page that is in a domain that is NOT the same as the originating domain of the running page, you must use this class, rather than HttpProxy.

The content passed back from a server resource requested by a ScriptTagProxy is executable JavaScript source code that is used as the source inside a <script> tag.

In order for the browser to process the returned data, the server must wrap the data object with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy. Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy depending on whether the callback name was passed:

 
 

boolean scriptTag = false; String cb = request.getParameter("callback"); if (cb != null) { scriptTag = true; response.setContentType("text/javascript"); } else { response.setContentType("application/x-json"); } Writer out = response.getWriter(); if (scriptTag) { out.write(cb + "("); } out.print(dataBlock.toJsonString()); if (scriptTag) { out.write(");"); }

Consider the following code :


  RecordDef recordDef = new RecordDef(new FieldDef[]{
             new StringFieldDef("name", "name"), // "mapping" property not needed if it's the same as "name"
             new StringFieldDef("occupation")    // this field will use "occupation" as the mapping.
     });
 

JsonReader reader = new JsonReader(new JsonReaderConfig() { { setTotalProperty("results"); // The property which contains the total dataset size (optional) setRoot("rows"); // The property which contains an Array of row objects setId("id"); // The property within each row object that provides an ID for the record (optional) }}, recordDef);

If the data is being server from the same domain, then you don;t need to use ScriptTagProxy but instead need to use HttpProxy pointing to the URL that returns data in the following format :

{ 'results': 2, 'rows': [
     { 'id': 1, 'name': 'Bill', occupation: 'Gardener' },
     { 'id': 2, 'name': 'Ben', occupation: 'Horticulturalist' } ]
 }

However if this data is being read from another domain, a couple of things need to be done.

First, you need to use ScriptTagProxy pointing to the external url. For example

 
 ScriptTagProxy proxy = new ScriptTagProxy("http://externalurl:8023/foo/bar.php");
 
 

Now bar.php cannot return standard Json like the one above when using HttpProxy. Instead it needs to wrap the Json data in a callback. This is required when reading data from antoher domain. When the ScriptTagProxy tries for fetch data, it will issue an HTTP request to the specified URL passing the name of the callback function that it needs the results to be wrapped in. For example http://externalurl:8023/foo/bar.php?start=0&limit=25&sort=nome&dir=DESC&_dc=1196661274168&callback=stcCallback1002
Notice the request paramter "callback" has a value of "stcCallback1002" in the above example. This means that the request to http://externalurl:8023/foo/bar.php should return Json data wrapped in the callback funtion "stcCallback1002".


stcCallback1002({ 'results': 2, 'rows': [
     { 'id': 1, 'name': 'Bill', occupation: 'Gardener' },
     { 'id': 2, 'name': 'Ben', occupation: 'Horticulturalist' }]);
 }

So for ScriptTagProxy to work, the URL that it is reading data from is responsible to read the "callback" paramter, and wrap the Json data using the name of the passed callback function. By default the name of the callback request parameter is "callback" but you can specify a different one using the constructor ScriptTagProxy(String, int, String).


Field Summary
 
Fields inherited from class com.gwtext.client.core.JsObject
jsObj
 
Constructor Summary
ScriptTagProxy(java.lang.String url)
          Construct a new ScriptTagProxy.
ScriptTagProxy(java.lang.String url, int timeout)
          Construct a new ScriptTagProxy.
ScriptTagProxy(java.lang.String url, int timeout, java.lang.String callbackParam)
          Construct a new ScriptTagProxy.
 
Method Summary
protected  com.google.gwt.core.client.JavaScriptObject create(com.google.gwt.core.client.JavaScriptObject config)
           
 
Methods inherited from class com.gwtext.client.core.JsObject
getJsObj, getProperties, isCreated, setJsObj
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ScriptTagProxy

public ScriptTagProxy(java.lang.String url)
Construct a new ScriptTagProxy.

Parameters:
url - the URL from which to request the data object.

ScriptTagProxy

public ScriptTagProxy(java.lang.String url,
                      int timeout)
Construct a new ScriptTagProxy.

Parameters:
url - the URL from which to request the data object.
timeout - the number of milliseconds to wait for a response. Defaults to 30 seconds.

ScriptTagProxy

public ScriptTagProxy(java.lang.String url,
                      int timeout,
                      java.lang.String callbackParam)
Construct a new ScriptTagProxy.

Parameters:
url - the URL from which to request the data object.
timeout - the number of milliseconds to wait for a response. Defaults to 30 seconds.
callbackParam - the name of the parameter to pass to the server which tells the server the name of the callback function set up by the load call to process the returned data object. Defaults to "callback". The server-side processing must read this parameter value, and generate javascript output which calls this named function passing the data object as its only parameter.
Method Detail

create

protected com.google.gwt.core.client.JavaScriptObject create(com.google.gwt.core.client.JavaScriptObject config)