4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.data.ScriptTagProxy
14 * An implementation of Roo.data.DataProxy that reads a data object from a URL which may be in a domain
15 * other than the originating domain of the running page.<br><br>
17 * <em>Note that if you are retrieving data from a page that is in a domain that is NOT the same as the originating domain
18 * of the running page, you must use this class, rather than DataProxy.</em><br><br>
20 * The content passed back from a server resource requested by a ScriptTagProxy is executable JavaScript
21 * source code that is used as the source inside a <script> tag.<br><br>
23 * In order for the browser to process the returned data, the server must wrap the data object
24 * with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy.
25 * Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy
26 * depending on whether the callback name was passed:
29 boolean scriptTag = false;
30 String cb = request.getParameter("callback");
33 response.setContentType("text/javascript");
35 response.setContentType("application/x-json");
37 Writer out = response.getWriter();
41 out.print(dataBlock.toJsonString());
48 * @param {Object} config A configuration object.
50 Roo.data.ScriptTagProxy = function(config){
51 Roo.data.ScriptTagProxy.superclass.constructor.call(this);
52 Roo.apply(this, config);
53 this.head = document.getElementsByTagName("head")[0];
56 Roo.data.ScriptTagProxy.TRANS_ID = 1000;
58 Roo.extend(Roo.data.ScriptTagProxy, Roo.data.DataProxy, {
60 * @cfg {String} url The URL from which to request the data object.
63 * @cfg {Number} timeout (Optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.
67 * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells
68 * the server the name of the callback function set up by the load call to process the returned data object.
69 * Defaults to "callback".<p>The server-side processing must read this parameter value, and generate
70 * javascript output which calls this named function passing the data object as its only parameter.
72 callbackParam : "callback",
74 * @cfg {Boolean} nocache (Optional) Defaults to true. Disable cacheing by adding a unique parameter
75 * name to the request.
80 * Load data from the configured URL, read the data object into
81 * a block of Roo.data.Records using the passed Roo.data.DataReader implementation, and
82 * process that block using the passed callback.
83 * @param {Object} params An object containing properties which are to be used as HTTP parameters
84 * for the request to the remote server.
85 * @param {Roo.data.DataReader} reader The Reader object which converts the data
86 * object into a block of Roo.data.Records.
87 * @param {Function} callback The function into which to pass the block of Roo.data.Records.
88 * The function must be passed <ul>
89 * <li>The Record block object</li>
90 * <li>The "arg" argument from the load function</li>
91 * <li>A boolean success indicator</li>
93 * @param {Object} scope The scope in which to call the callback
94 * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
96 load : function(params, reader, callback, scope, arg){
97 if(this.fireEvent("beforeload", this, params) !== false){
99 var p = Roo.urlEncode(Roo.apply(params, this.extraParams));
102 url += (url.indexOf("?") != -1 ? "&" : "?") + p;
104 url += "&_dc=" + (new Date().getTime());
106 var transId = ++Roo.data.ScriptTagProxy.TRANS_ID;
109 cb : "stcCallback"+transId,
110 scriptId : "stcScript"+transId,
120 window[trans.cb] = function(o){
121 conn.handleResponse(o, trans);
124 url += String.format("&{0}={1}", this.callbackParam, trans.cb);
126 if(this.autoAbort !== false){
130 trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);
132 var script = document.createElement("script");
133 script.setAttribute("src", url);
134 script.setAttribute("type", "text/javascript");
135 script.setAttribute("id", trans.scriptId);
136 this.head.appendChild(script);
140 callback.call(scope||this, null, arg, false);
145 isLoading : function(){
146 return this.trans ? true : false;
150 * Abort the current server request.
153 if(this.isLoading()){
154 this.destroyTrans(this.trans);
159 destroyTrans : function(trans, isLoaded){
160 this.head.removeChild(document.getElementById(trans.scriptId));
161 clearTimeout(trans.timeoutId);
163 window[trans.cb] = undefined;
165 delete window[trans.cb];
168 // if hasn't been loaded, wait for load to remove it to prevent script error
169 window[trans.cb] = function(){
170 window[trans.cb] = undefined;
172 delete window[trans.cb];
179 handleResponse : function(o, trans){
181 this.destroyTrans(trans, true);
184 result = trans.reader.readRecords(o);
186 this.fireEvent("loadexception", this, o, trans.arg, e);
187 trans.callback.call(trans.scope||window, null, trans.arg, false);
190 this.fireEvent("load", this, o, trans.arg);
191 trans.callback.call(trans.scope||window, result, trans.arg, true);
195 handleFailure : function(trans){
197 this.destroyTrans(trans, false);
198 this.fireEvent("loadexception", this, null, trans.arg);
199 trans.callback.call(trans.scope||window, null, trans.arg, false);