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