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