Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / ScriptTagProxy.html
1 <html>
2 <head>
3   <title>The source code</title>
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
6 </head>
7 <body  onload="prettyPrint();">
8     <pre class="prettyprint lang-js">/*!
9  * Ext JS Library 3.0.3
10  * Copyright(c) 2006-2009 Ext JS, LLC
11  * licensing@extjs.com
12  * http://www.extjs.com/license
13  */
14 <div id="cls-Ext.data.ScriptTagProxy"></div>/**\r
15  * @class Ext.data.ScriptTagProxy\r
16  * @extends Ext.data.DataProxy\r
17  * An implementation of Ext.data.DataProxy that reads a data object from a URL which may be in a domain\r
18  * other than the originating domain of the running page.<br>\r
19  * <p>\r
20  * <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
21  * of the running page, you must use this class, rather than HttpProxy.</b><br>\r
22  * <p>\r
23  * The content passed back from a server resource requested by a ScriptTagProxy <b>must</b> be executable JavaScript\r
24  * source code because it is used as the source inside a &lt;script> tag.<br>\r
25  * <p>\r
26  * In order for the browser to process the returned data, the server must wrap the data object\r
27  * with a call to a callback function, the name of which is passed as a parameter by the ScriptTagProxy.\r
28  * Below is a Java example for a servlet which returns data for either a ScriptTagProxy, or an HttpProxy\r
29  * depending on whether the callback name was passed:\r
30  * <p>\r
31  * <pre><code>\r
32 boolean scriptTag = false;\r
33 String cb = request.getParameter("callback");\r
34 if (cb != null) {\r
35     scriptTag = true;\r
36     response.setContentType("text/javascript");\r
37 } else {\r
38     response.setContentType("application/x-json");\r
39 }\r
40 Writer out = response.getWriter();\r
41 if (scriptTag) {\r
42     out.write(cb + "(");\r
43 }\r
44 out.print(dataBlock.toJsonString());\r
45 if (scriptTag) {\r
46     out.write(");");\r
47 }\r
48 </code></pre>\r
49  *\r
50  * @constructor\r
51  * @param {Object} config A configuration object.\r
52  */\r
53 Ext.data.ScriptTagProxy = function(config){\r
54     Ext.apply(this, config);\r
55 \r
56     Ext.data.ScriptTagProxy.superclass.constructor.call(this, config);\r
57 \r
58     this.head = document.getElementsByTagName("head")[0];\r
59 \r
60     <div id="event-Ext.data.ScriptTagProxy-loadexception"></div>/**\r
61      * @event loadexception\r
62      * <b>Deprecated</b> in favor of 'exception' event.\r
63      * Fires if an exception occurs in the Proxy during data loading.  This event can be fired for one of two reasons:\r
64      * <ul><li><b>The load call timed out.</b>  This means the load callback did not execute within the time limit\r
65      * specified by {@link #timeout}.  In this case, this event will be raised and the\r
66      * fourth parameter (read error) will be null.</li>\r
67      * <li><b>The load succeeded but the reader could not read the response.</b>  This means the server returned\r
68      * data, but the configured Reader threw an error while reading the data.  In this case, this event will be\r
69      * raised and the caught error will be passed along as the fourth parameter of this event.</li></ul>\r
70      * Note that this event is also relayed through {@link Ext.data.Store}, so you can listen for it directly\r
71      * on any Store instance.\r
72      * @param {Object} this\r
73      * @param {Object} options The loading options that were specified (see {@link #load} for details).  If the load\r
74      * call timed out, this parameter will be null.\r
75      * @param {Object} arg The callback's arg object passed to the {@link #load} function\r
76      * @param {Error} e The JavaScript Error object caught if the configured Reader could not read the data.\r
77      * If the remote request returns success: false, this parameter will be null.\r
78      */\r
79 };\r
80 \r
81 Ext.data.ScriptTagProxy.TRANS_ID = 1000;\r
82 \r
83 Ext.extend(Ext.data.ScriptTagProxy, Ext.data.DataProxy, {\r
84     <div id="cfg-Ext.data.ScriptTagProxy-url"></div>/**\r
85      * @cfg {String} url The URL from which to request the data object.\r
86      */\r
87     <div id="cfg-Ext.data.ScriptTagProxy-timeout"></div>/**\r
88      * @cfg {Number} timeout (optional) The number of milliseconds to wait for a response. Defaults to 30 seconds.\r
89      */\r
90     timeout : 30000,\r
91     <div id="cfg-Ext.data.ScriptTagProxy-callbackParam"></div>/**\r
92      * @cfg {String} callbackParam (Optional) The name of the parameter to pass to the server which tells\r
93      * the server the name of the callback function set up by the load call to process the returned data object.\r
94      * Defaults to "callback".<p>The server-side processing must read this parameter value, and generate\r
95      * javascript output which calls this named function passing the data object as its only parameter.\r
96      */\r
97     callbackParam : "callback",\r
98     <div id="cfg-Ext.data.ScriptTagProxy-nocache"></div>/**\r
99      *  @cfg {Boolean} nocache (optional) Defaults to true. Disable caching by adding a unique parameter\r
100      * name to the request.\r
101      */\r
102     nocache : true,\r
103 \r
104     <div id="method-Ext.data.ScriptTagProxy-doRequest"></div>/**\r
105      * HttpProxy implementation of DataProxy#doRequest\r
106      * @param {String} action\r
107      * @param {Ext.data.Record/Ext.data.Record[]} rs If action is <tt>read</tt>, rs will be null\r
108      * @param {Object} params An object containing properties which are to be used as HTTP parameters\r
109      * for the request to the remote server.\r
110      * @param {Ext.data.DataReader} reader The Reader object which converts the data\r
111      * object into a block of Ext.data.Records.\r
112      * @param {Function} callback The function into which to pass the block of Ext.data.Records.\r
113      * The function must be passed <ul>\r
114      * <li>The Record block object</li>\r
115      * <li>The "arg" argument from the load function</li>\r
116      * <li>A boolean success indicator</li>\r
117      * </ul>\r
118      * @param {Object} scope The scope in which to call the callback\r
119      * @param {Object} arg An optional argument which is passed to the callback as its second parameter.\r
120      */\r
121     doRequest : function(action, rs, params, reader, callback, scope, arg) {\r
122         var p = Ext.urlEncode(Ext.apply(params, this.extraParams));\r
123 \r
124         var url = this.buildUrl(action, rs);\r
125         if (!url) {\r
126             throw new Ext.data.Api.Error('invalid-url', url);\r
127         }\r
128         url = Ext.urlAppend(url, p);\r
129 \r
130         if(this.nocache){\r
131             url = Ext.urlAppend(url, '_dc=' + (new Date().getTime()));\r
132         }\r
133         var transId = ++Ext.data.ScriptTagProxy.TRANS_ID;\r
134         var trans = {\r
135             id : transId,\r
136             action: action,\r
137             cb : "stcCallback"+transId,\r
138             scriptId : "stcScript"+transId,\r
139             params : params,\r
140             arg : arg,\r
141             url : url,\r
142             callback : callback,\r
143             scope : scope,\r
144             reader : reader\r
145         };\r
146         window[trans.cb] = this.createCallback(action, rs, trans);\r
147         url += String.format("&{0}={1}", this.callbackParam, trans.cb);\r
148         if(this.autoAbort !== false){\r
149             this.abort();\r
150         }\r
151 \r
152         trans.timeoutId = this.handleFailure.defer(this.timeout, this, [trans]);\r
153 \r
154         var script = document.createElement("script");\r
155         script.setAttribute("src", url);\r
156         script.setAttribute("type", "text/javascript");\r
157         script.setAttribute("id", trans.scriptId);\r
158         this.head.appendChild(script);\r
159 \r
160         this.trans = trans;\r
161     },\r
162 \r
163     // @private createCallback\r
164     createCallback : function(action, rs, trans) {\r
165         var self = this;\r
166         return function(res) {\r
167             self.trans = false;\r
168             self.destroyTrans(trans, true);\r
169             if (action === Ext.data.Api.actions.read) {\r
170                 self.onRead.call(self, action, trans, res);\r
171             } else {\r
172                 self.onWrite.call(self, action, trans, res, rs);\r
173             }\r
174         };\r
175     },\r
176     /**\r
177      * Callback for read actions\r
178      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
179      * @param {Object} trans The request transaction object\r
180      * @param {Object} res The server response\r
181      * @private\r
182      */\r
183     onRead : function(action, trans, res) {\r
184         var result;\r
185         try {\r
186             result = trans.reader.readRecords(res);\r
187         }catch(e){\r
188             // @deprecated: fire loadexception\r
189             this.fireEvent("loadexception", this, trans, res, e);\r
190 \r
191             this.fireEvent('exception', this, 'response', action, trans, res, e);\r
192             trans.callback.call(trans.scope||window, null, trans.arg, false);\r
193             return;\r
194         }\r
195         if (result.success === false) {\r
196             // @deprecated: fire old loadexception for backwards-compat.\r
197             this.fireEvent('loadexception', this, trans, res);\r
198 \r
199             this.fireEvent('exception', this, 'remote', action, trans, res, null);\r
200         } else {\r
201             this.fireEvent("load", this, res, trans.arg);\r
202         }\r
203         trans.callback.call(trans.scope||window, result, trans.arg, result.success);\r
204     },\r
205     /**\r
206      * Callback for write actions\r
207      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
208      * @param {Object} trans The request transaction object\r
209      * @param {Object} res The server response\r
210      * @private\r
211      */\r
212     onWrite : function(action, trans, response, rs) {\r
213         var reader = trans.reader;\r
214         try {\r
215             // though we already have a response object here in STP, run through readResponse to catch any meta-data exceptions.\r
216             var res = reader.readResponse(action, response);\r
217         } catch (e) {\r
218             this.fireEvent('exception', this, 'response', action, trans, res, e);\r
219             trans.callback.call(trans.scope||window, null, res, false);\r
220             return;\r
221         }\r
222         if(!res.success === true){\r
223             this.fireEvent('exception', this, 'remote', action, trans, res, rs);\r
224             trans.callback.call(trans.scope||window, null, res, false);\r
225             return;\r
226         }\r
227         this.fireEvent("write", this, action, res.data, res, rs, trans.arg );\r
228         trans.callback.call(trans.scope||window, res.data, res, true);\r
229     },\r
230 \r
231     // private\r
232     isLoading : function(){\r
233         return this.trans ? true : false;\r
234     },\r
235 \r
236     <div id="method-Ext.data.ScriptTagProxy-abort"></div>/**\r
237      * Abort the current server request.\r
238      */\r
239     abort : function(){\r
240         if(this.isLoading()){\r
241             this.destroyTrans(this.trans);\r
242         }\r
243     },\r
244 \r
245     // private\r
246     destroyTrans : function(trans, isLoaded){\r
247         this.head.removeChild(document.getElementById(trans.scriptId));\r
248         clearTimeout(trans.timeoutId);\r
249         if(isLoaded){\r
250             window[trans.cb] = undefined;\r
251             try{\r
252                 delete window[trans.cb];\r
253             }catch(e){}\r
254         }else{\r
255             // if hasn't been loaded, wait for load to remove it to prevent script error\r
256             window[trans.cb] = function(){\r
257                 window[trans.cb] = undefined;\r
258                 try{\r
259                     delete window[trans.cb];\r
260                 }catch(e){}\r
261             };\r
262         }\r
263     },\r
264 \r
265     // private\r
266     handleFailure : function(trans){\r
267         this.trans = false;\r
268         this.destroyTrans(trans, false);\r
269         if (trans.action === Ext.data.Api.actions.read) {\r
270             // @deprecated firing loadexception\r
271             this.fireEvent("loadexception", this, null, trans.arg);\r
272         }\r
273 \r
274         this.fireEvent('exception', this, 'response', trans.action, {\r
275             response: null,\r
276             options: trans.arg\r
277         });\r
278         trans.callback.call(trans.scope||window, null, trans.arg, false);\r
279     },\r
280 \r
281     // inherit docs\r
282     destroy: function(){\r
283         this.abort();\r
284         Ext.data.ScriptTagProxy.superclass.destroy.call(this);\r
285     }\r
286 });</pre>
287 </body>
288 </html>