2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.data.ScriptTagProxy
\r
11 * @extends Ext.data.DataProxy
\r
12 * An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain
\r
13 * other than the originating domain of the running page.<br>
\r
15 * <b>Note that if you are retrieving data from a page that is in a domain that is NOT the same as the originating domain
\r
16 * of the running page, you must use this class, rather than HttpProxy.</b><br>
\r
18 * The content passed back from a server resource requested by a ScriptTagProxy <b>must</b> be executable JavaScript
\r
19 * source code because it is used as the source inside a <script> tag.<br>
\r
21 * In order for the browser to process the returned data, the server must wrap the data object
\r
22 * with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy.
\r
23 * Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy
\r
24 * depending on whether the callback name was passed:
\r
27 boolean scriptTag = false;
\r
28 String cb = request.getParameter("callback");
\r
31 response.setContentType("text/javascript");
\r
33 response.setContentType("application/x-json");
\r
35 Writer out = response.getWriter();
\r
37 out.write(cb + "(");
\r
39 out.print(dataBlock.toJsonString());
\r
46 * @param {Object} config A configuration object.
\r
48 Ext.data.ScriptTagProxy = function(config){
\r
49 Ext.data.ScriptTagProxy.superclass.constructor.call(this);
\r
50 Ext.apply(this, config);
\r
51 this.head = document.getElementsByTagName("head")[0];
\r
54 * @event loadexception
\r
55 * Fires if an exception occurs in the Proxy during data loading. This event can be fired for one of two reasons:
\r
56 * <ul><li><b>The load call timed out.</b> This means the load callback did not execute within the time limit
\r
57 * specified by {@link #timeout}. In this case, this event will be raised and the
\r
58 * fourth parameter (read error) will be null.</li>
\r
59 * <li><b>The load succeeded but the reader could not read the response.</b> This means the server returned
\r
60 * data, but the configured Reader threw an error while reading the data. In this case, this event will be
\r
61 * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul>
\r
62 * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly
\r
63 * on any Store instance.
\r
64 * @param {Object} this
\r
65 * @param {Object} options The loading options that were specified (see {@link #load} for details). If the load
\r
66 * call timed out, this parameter will be null.
\r
67 * @param {Object} arg The callback's arg object passed to the {@link #load} function
\r
68 * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data.
\r
69 * If the load call returned success: false, this parameter will be null.
\r
73 Ext.data.ScriptTagProxy.TRANS_ID = 1000;
\r
75 Ext.extend(Ext.data.ScriptTagProxy, Ext.data.DataProxy, {
\r
77 * @cfg {String} url The URL from which to request the data object.
\r
80 * @cfg {Number} timeout (optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.
\r
84 * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells
\r
85 * the server the name of the callback function set up by the load call to process the returned data object.
\r
86 * Defaults to "callback".<p>The server-side processing must read this parameter value, and generate
\r
87 * javascript output which calls this named function passing the data object as its only parameter.
\r
89 callbackParam : "callback",
\r
91 * @cfg {Boolean} nocache (optional) Defaults to true. Disable caching by adding a unique parameter
\r
92 * name to the request.
\r
97 * Load data from the configured URL, read the data object into
\r
98 * a block of Ext.data.Records using the passed Ext.data.DataReader implementation, and
\r
99 * process that block using the passed callback.
\r
100 * @param {Object} params An object containing properties which are to be used as HTTP parameters
\r
101 * for the request to the remote server.
\r
102 * @param {Ext.data.DataReader} reader The Reader object which converts the data
\r
103 * object into a block of Ext.data.Records.
\r
104 * @param {Function} callback The function into which to pass the block of Ext.data.Records.
\r
105 * The function must be passed <ul>
\r
106 * <li>The Record block object</li>
\r
107 * <li>The "arg" argument from the load function</li>
\r
108 * <li>A boolean success indicator</li>
\r
110 * @param {Object} scope The scope in which to call the callback
\r
111 * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
\r
113 load : function(params, reader, callback, scope, arg){
\r
114 if(this.fireEvent("beforeload", this, params) !== false){
\r
116 var p = Ext.urlEncode(Ext.apply(params, this.extraParams));
\r
118 var url = this.url;
\r
119 url += (url.indexOf("?") != -1 ? "&" : "?") + p;
\r
121 url += "&_dc=" + (new Date().getTime());
\r
123 var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;
\r
126 cb : "stcCallback"+transId,
\r
127 scriptId : "stcScript"+transId,
\r
131 callback : callback,
\r
137 window[trans.cb] = function(o){
\r
138 conn.handleResponse(o, trans);
\r
141 url += String.format("&{0}={1}", this.callbackParam, trans.cb);
\r
143 if(this.autoAbort !== false){
\r
147 trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);
\r
149 var script = document.createElement("script");
\r
150 script.setAttribute("src", url);
\r
151 script.setAttribute("type", "text/javascript");
\r
152 script.setAttribute("id", trans.scriptId);
\r
153 this.head.appendChild(script);
\r
155 this.trans = trans;
\r
157 callback.call(scope||this, null, arg, false);
\r
162 isLoading : function(){
\r
163 return this.trans ? true : false;
\r
167 * Abort the current server request.
\r
169 abort : function(){
\r
170 if(this.isLoading()){
\r
171 this.destroyTrans(this.trans);
\r
176 destroyTrans : function(trans, isLoaded){
\r
177 this.head.removeChild(document.getElementById(trans.scriptId));
\r
178 clearTimeout(trans.timeoutId);
\r
180 window[trans.cb] = undefined;
\r
182 delete window[trans.cb];
\r
185 // if hasn't been loaded, wait for load to remove it to prevent script error
\r
186 window[trans.cb] = function(){
\r
187 window[trans.cb] = undefined;
\r
189 delete window[trans.cb];
\r
196 handleResponse : function(o, trans){
\r
197 this.trans = false;
\r
198 this.destroyTrans(trans, true);
\r
201 result = trans.reader.readRecords(o);
\r
203 this.fireEvent("loadexception", this, o, trans.arg, e);
\r
204 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
207 this.fireEvent("load", this, o, trans.arg);
\r
208 trans.callback.call(trans.scope||window, result, trans.arg, true);
\r
212 handleFailure : function(trans){
\r
213 this.trans = false;
\r
214 this.destroyTrans(trans, false);
\r
215 this.fireEvent("loadexception", this, null, trans.arg);
\r
216 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r