3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.data.ScriptTagProxy
\r
9 * @extends Ext.data.DataProxy
\r
10 * An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain
\r
11 * other than the originating domain of the running page.<br>
\r
13 * <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
14 * of the running page, you must use this class, rather than HttpProxy.</b><br>
\r
16 * The content passed back from a server resource requested by a ScriptTagProxy <b>must</b> be executable JavaScript
\r
17 * source code because it is used as the source inside a <script> tag.<br>
\r
19 * In order for the browser to process the returned data, the server must wrap the data object
\r
20 * with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy.
\r
21 * Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy
\r
22 * depending on whether the callback name was passed:
\r
25 boolean scriptTag = false;
\r
26 String cb = request.getParameter("callback");
\r
29 response.setContentType("text/javascript");
\r
31 response.setContentType("application/x-json");
\r
33 Writer out = response.getWriter();
\r
35 out.write(cb + "(");
\r
37 out.print(dataBlock.toJsonString());
\r
42 * <p>Below is a PHP example to do the same thing:</p><pre><code>
\r
43 $callback = $_REQUEST['callback'];
\r
45 // Create the output object.
\r
46 $output = array('a' => 'Apple', 'b' => 'Banana');
\r
50 header('Content-Type: text/javascript');
\r
51 echo $callback . '(' . json_encode($output) . ');';
\r
53 header('Content-Type: application/x-json');
\r
54 echo json_encode($output);
\r
59 * @param {Object} config A configuration object.
\r
61 Ext.data.ScriptTagProxy = function(config){
\r
62 Ext.apply(this, config);
\r
64 Ext.data.ScriptTagProxy.superclass.constructor.call(this, config);
\r
66 this.head = document.getElementsByTagName("head")[0];
\r
69 * @event loadexception
\r
70 * <b>Deprecated</b> in favor of 'exception' event.
\r
71 * Fires if an exception occurs in the Proxy during data loading. This event can be fired for one of two reasons:
\r
72 * <ul><li><b>The load call timed out.</b> This means the load callback did not execute within the time limit
\r
73 * specified by {@link #timeout}. In this case, this event will be raised and the
\r
74 * fourth parameter (read error) will be null.</li>
\r
75 * <li><b>The load succeeded but the reader could not read the response.</b> This means the server returned
\r
76 * data, but the configured Reader threw an error while reading the data. In this case, this event will be
\r
77 * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul>
\r
78 * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly
\r
79 * on any Store instance.
\r
80 * @param {Object} this
\r
81 * @param {Object} options The loading options that were specified (see {@link #load} for details). If the load
\r
82 * call timed out, this parameter will be null.
\r
83 * @param {Object} arg The callback's arg object passed to the {@link #load} function
\r
84 * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data.
\r
85 * If the remote request returns success: false, this parameter will be null.
\r
89 Ext.data.ScriptTagProxy.TRANS_ID = 1000;
\r
91 Ext.extend(Ext.data.ScriptTagProxy, Ext.data.DataProxy, {
\r
93 * @cfg {String} url The URL from which to request the data object.
\r
96 * @cfg {Number} timeout (optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.
\r
100 * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells
\r
101 * the server the name of the callback function set up by the load call to process the returned data object.
\r
102 * Defaults to "callback".<p>The server-side processing must read this parameter value, and generate
\r
103 * javascript output which calls this named function passing the data object as its only parameter.
\r
105 callbackParam : "callback",
\r
107 * @cfg {Boolean} nocache (optional) Defaults to true. Disable caching by adding a unique parameter
\r
108 * name to the request.
\r
113 * HttpProxy implementation of DataProxy#doRequest
\r
114 * @param {String} action
\r
115 * @param {Ext.data.Record/Ext.data.Record[]} rs If action is <tt>read</tt>, rs will be null
\r
116 * @param {Object} params An object containing properties which are to be used as HTTP parameters
\r
117 * for the request to the remote server.
\r
118 * @param {Ext.data.DataReader} reader The Reader object which converts the data
\r
119 * object into a block of Ext.data.Records.
\r
120 * @param {Function} callback The function into which to pass the block of Ext.data.Records.
\r
121 * The function must be passed <ul>
\r
122 * <li>The Record block object</li>
\r
123 * <li>The "arg" argument from the load function</li>
\r
124 * <li>A boolean success indicator</li>
\r
126 * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
\r
127 * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
\r
129 doRequest : function(action, rs, params, reader, callback, scope, arg) {
\r
130 var p = Ext.urlEncode(Ext.apply(params, this.extraParams));
\r
132 var url = this.buildUrl(action, rs);
\r
134 throw new Ext.data.Api.Error('invalid-url', url);
\r
136 url = Ext.urlAppend(url, p);
\r
139 url = Ext.urlAppend(url, '_dc=' + (new Date().getTime()));
\r
141 var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;
\r
145 cb : "stcCallback"+transId,
\r
146 scriptId : "stcScript"+transId,
\r
150 callback : callback,
\r
154 window[trans.cb] = this.createCallback(action, rs, trans);
\r
155 url += String.format("&{0}={1}", this.callbackParam, trans.cb);
\r
156 if(this.autoAbort !== false){
\r
160 trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);
\r
162 var script = document.createElement("script");
\r
163 script.setAttribute("src", url);
\r
164 script.setAttribute("type", "text/javascript");
\r
165 script.setAttribute("id", trans.scriptId);
\r
166 this.head.appendChild(script);
\r
168 this.trans = trans;
\r
171 // @private createCallback
\r
172 createCallback : function(action, rs, trans) {
\r
174 return function(res) {
\r
175 self.trans = false;
\r
176 self.destroyTrans(trans, true);
\r
177 if (action === Ext.data.Api.actions.read) {
\r
178 self.onRead.call(self, action, trans, res);
\r
180 self.onWrite.call(self, action, trans, res, rs);
\r
185 * Callback for read actions
\r
186 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
\r
187 * @param {Object} trans The request transaction object
\r
188 * @param {Object} res The server response
\r
191 onRead : function(action, trans, res) {
\r
194 result = trans.reader.readRecords(res);
\r
196 // @deprecated: fire loadexception
\r
197 this.fireEvent("loadexception", this, trans, res, e);
\r
199 this.fireEvent('exception', this, 'response', action, trans, res, e);
\r
200 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
203 if (result.success === false) {
\r
204 // @deprecated: fire old loadexception for backwards-compat.
\r
205 this.fireEvent('loadexception', this, trans, res);
\r
207 this.fireEvent('exception', this, 'remote', action, trans, res, null);
\r
209 this.fireEvent("load", this, res, trans.arg);
\r
211 trans.callback.call(trans.scope||window, result, trans.arg, result.success);
\r
214 * Callback for write actions
\r
215 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
\r
216 * @param {Object} trans The request transaction object
\r
217 * @param {Object} res The server response
\r
220 onWrite : function(action, trans, response, rs) {
\r
221 var reader = trans.reader;
\r
223 // though we already have a response object here in STP, run through readResponse to catch any meta-data exceptions.
\r
224 var res = reader.readResponse(action, response);
\r
226 this.fireEvent('exception', this, 'response', action, trans, res, e);
\r
227 trans.callback.call(trans.scope||window, null, res, false);
\r
230 if(!res.success === true){
\r
231 this.fireEvent('exception', this, 'remote', action, trans, res, rs);
\r
232 trans.callback.call(trans.scope||window, null, res, false);
\r
235 this.fireEvent("write", this, action, res.data, res, rs, trans.arg );
\r
236 trans.callback.call(trans.scope||window, res.data, res, true);
\r
240 isLoading : function(){
\r
241 return this.trans ? true : false;
\r
245 * Abort the current server request.
\r
247 abort : function(){
\r
248 if(this.isLoading()){
\r
249 this.destroyTrans(this.trans);
\r
254 destroyTrans : function(trans, isLoaded){
\r
255 this.head.removeChild(document.getElementById(trans.scriptId));
\r
256 clearTimeout(trans.timeoutId);
\r
258 window[trans.cb] = undefined;
\r
260 delete window[trans.cb];
\r
263 // if hasn't been loaded, wait for load to remove it to prevent script error
\r
264 window[trans.cb] = function(){
\r
265 window[trans.cb] = undefined;
\r
267 delete window[trans.cb];
\r
274 handleFailure : function(trans){
\r
275 this.trans = false;
\r
276 this.destroyTrans(trans, false);
\r
277 if (trans.action === Ext.data.Api.actions.read) {
\r
278 // @deprecated firing loadexception
\r
279 this.fireEvent("loadexception", this, null, trans.arg);
\r
282 this.fireEvent('exception', this, 'response', trans.action, {
\r
286 trans.callback.call(trans.scope||window, null, trans.arg, false);
\r
290 destroy: function(){
\r
292 Ext.data.ScriptTagProxy.superclass.destroy.call(this);
\r