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.direct.RemotingProvider |
---|
9 | * @extends Ext.direct.JsonProvider |
---|
10 | * |
---|
11 | * <p>The {@link Ext.direct.RemotingProvider RemotingProvider} exposes access to |
---|
12 | * server side methods on the client (a remote procedure call (RPC) type of |
---|
13 | * connection where the client can initiate a procedure on the server).</p> |
---|
14 | * |
---|
15 | * <p>This allows for code to be organized in a fashion that is maintainable, |
---|
16 | * while providing a clear path between client and server, something that is |
---|
17 | * not always apparent when using URLs.</p> |
---|
18 | * |
---|
19 | * <p>To accomplish this the server-side needs to describe what classes and methods |
---|
20 | * are available on the client-side. This configuration will typically be |
---|
21 | * outputted by the server-side Ext.Direct stack when the API description is built.</p> |
---|
22 | */ |
---|
23 | Ext.direct.RemotingProvider = Ext.extend(Ext.direct.JsonProvider, { |
---|
24 | /** |
---|
25 | * @cfg {Object} actions |
---|
26 | * Object literal defining the server side actions and methods. For example, if |
---|
27 | * the Provider is configured with: |
---|
28 | * <pre><code> |
---|
29 | "actions":{ // each property within the 'actions' object represents a server side Class |
---|
30 | "TestAction":[ // array of methods within each server side Class to be |
---|
31 | { // stubbed out on client |
---|
32 | "name":"doEcho", |
---|
33 | "len":1 |
---|
34 | },{ |
---|
35 | "name":"multiply",// name of method |
---|
36 | "len":2 // The number of parameters that will be used to create an |
---|
37 | // array of data to send to the server side function. |
---|
38 | // Ensure the server sends back a Number, not a String. |
---|
39 | },{ |
---|
40 | "name":"doForm", |
---|
41 | "formHandler":true, // direct the client to use specialized form handling method |
---|
42 | "len":1 |
---|
43 | }] |
---|
44 | } |
---|
45 | * </code></pre> |
---|
46 | * <p>Note that a Store is not required, a server method can be called at any time. |
---|
47 | * In the following example a <b>client side</b> handler is used to call the |
---|
48 | * server side method "multiply" in the server-side "TestAction" Class:</p> |
---|
49 | * <pre><code> |
---|
50 | TestAction.multiply( |
---|
51 | 2, 4, // pass two arguments to server, so specify len=2 |
---|
52 | // callback function after the server is called |
---|
53 | // result: the result returned by the server |
---|
54 | // e: Ext.Direct.RemotingEvent object |
---|
55 | function(result, e){ |
---|
56 | var t = e.getTransaction(); |
---|
57 | var action = t.action; // server side Class called |
---|
58 | var method = t.method; // server side method called |
---|
59 | if(e.status){ |
---|
60 | var answer = Ext.encode(result); // 8 |
---|
61 | |
---|
62 | }else{ |
---|
63 | var msg = e.message; // failure message |
---|
64 | } |
---|
65 | } |
---|
66 | ); |
---|
67 | * </code></pre> |
---|
68 | * In the example above, the server side "multiply" function will be passed two |
---|
69 | * arguments (2 and 4). The "multiply" method should return the value 8 which will be |
---|
70 | * available as the <tt>result</tt> in the example above. |
---|
71 | */ |
---|
72 | |
---|
73 | /** |
---|
74 | * @cfg {String/Object} namespace |
---|
75 | * Namespace for the Remoting Provider (defaults to the browser global scope of <i>window</i>). |
---|
76 | * Explicitly specify the namespace Object, or specify a String to have a |
---|
77 | * {@link Ext#namespace namespace created} implicitly. |
---|
78 | */ |
---|
79 | |
---|
80 | /** |
---|
81 | * @cfg {String} url |
---|
82 | * <b>Required<b>. The url to connect to the {@link Ext.Direct} server-side router. |
---|
83 | */ |
---|
84 | |
---|
85 | /** |
---|
86 | * @cfg {String} enableUrlEncode |
---|
87 | * Specify which param will hold the arguments for the method. |
---|
88 | * Defaults to <tt>'data'</tt>. |
---|
89 | */ |
---|
90 | |
---|
91 | /** |
---|
92 | * @cfg {Number/Boolean} enableBuffer |
---|
93 | * <p><tt>true</tt> or <tt>false</tt> to enable or disable combining of method |
---|
94 | * calls. If a number is specified this is the amount of time in milliseconds |
---|
95 | * to wait before sending a batched request (defaults to <tt>10</tt>).</p> |
---|
96 | * <br><p>Calls which are received within the specified timeframe will be |
---|
97 | * concatenated together and sent in a single request, optimizing the |
---|
98 | * application by reducing the amount of round trips that have to be made |
---|
99 | * to the server.</p> |
---|
100 | */ |
---|
101 | enableBuffer: 10, |
---|
102 | |
---|
103 | /** |
---|
104 | * @cfg {Number} maxRetries |
---|
105 | * Number of times to re-attempt delivery on failure of a call. |
---|
106 | */ |
---|
107 | maxRetries: 1, |
---|
108 | |
---|
109 | constructor : function(config){ |
---|
110 | Ext.direct.RemotingProvider.superclass.constructor.call(this, config); |
---|
111 | this.addEvents( |
---|
112 | /** |
---|
113 | * @event beforecall |
---|
114 | * Fires immediately before the client-side sends off the RPC call. |
---|
115 | * By returning false from an event handler you can prevent the call from |
---|
116 | * executing. |
---|
117 | * @param {Ext.direct.RemotingProvider} provider |
---|
118 | * @param {Ext.Direct.Transaction} transaction |
---|
119 | */ |
---|
120 | 'beforecall', |
---|
121 | /** |
---|
122 | * @event call |
---|
123 | * Fires immediately after the request to the server-side is sent. This does |
---|
124 | * NOT fire after the response has come back from the call. |
---|
125 | * @param {Ext.direct.RemotingProvider} provider |
---|
126 | * @param {Ext.Direct.Transaction} transaction |
---|
127 | */ |
---|
128 | 'call' |
---|
129 | ); |
---|
130 | this.namespace = (typeof this.namespace === 'string') ? Ext.ns(this.namespace) : this.namespace || window; |
---|
131 | this.transactions = {}; |
---|
132 | this.callBuffer = []; |
---|
133 | }, |
---|
134 | |
---|
135 | // private |
---|
136 | initAPI : function(){ |
---|
137 | var o = this.actions; |
---|
138 | for(var c in o){ |
---|
139 | var cls = this.namespace[c] || (this.namespace[c] = {}); |
---|
140 | var ms = o[c]; |
---|
141 | for(var i = 0, len = ms.length; i < len; i++){ |
---|
142 | var m = ms[i]; |
---|
143 | cls[m.name] = this.createMethod(c, m); |
---|
144 | } |
---|
145 | } |
---|
146 | }, |
---|
147 | |
---|
148 | // inherited |
---|
149 | isConnected: function(){ |
---|
150 | return !!this.connected; |
---|
151 | }, |
---|
152 | |
---|
153 | connect: function(){ |
---|
154 | if(this.url){ |
---|
155 | this.initAPI(); |
---|
156 | this.connected = true; |
---|
157 | this.fireEvent('connect', this); |
---|
158 | }else if(!this.url){ |
---|
159 | throw 'Error initializing RemotingProvider, no url configured.'; |
---|
160 | } |
---|
161 | }, |
---|
162 | |
---|
163 | disconnect: function(){ |
---|
164 | if(this.connected){ |
---|
165 | this.connected = false; |
---|
166 | this.fireEvent('disconnect', this); |
---|
167 | } |
---|
168 | }, |
---|
169 | |
---|
170 | onData: function(opt, success, xhr){ |
---|
171 | if(success){ |
---|
172 | var events = this.getEvents(xhr); |
---|
173 | for(var i = 0, len = events.length; i < len; i++){ |
---|
174 | var e = events[i]; |
---|
175 | var t = this.getTransaction(e); |
---|
176 | this.fireEvent('data', this, e); |
---|
177 | if(t){ |
---|
178 | this.doCallback(t, e, true); |
---|
179 | Ext.Direct.removeTransaction(t); |
---|
180 | } |
---|
181 | } |
---|
182 | }else{ |
---|
183 | var ts = [].concat(opt.ts); |
---|
184 | for(var i = 0, len = ts.length; i < len; i++){ |
---|
185 | var t = this.getTransaction(ts[i]); |
---|
186 | if(t && t.retryCount < this.maxRetries){ |
---|
187 | t.retry(); |
---|
188 | }else{ |
---|
189 | var e = new Ext.Direct.ExceptionEvent({ |
---|
190 | data: e, |
---|
191 | transaction: t, |
---|
192 | code: Ext.Direct.exceptions.TRANSPORT, |
---|
193 | message: 'Unable to connect to the server.', |
---|
194 | xhr: xhr |
---|
195 | }); |
---|
196 | this.fireEvent('data', this, e); |
---|
197 | if(t){ |
---|
198 | this.doCallback(t, e, false); |
---|
199 | Ext.Direct.removeTransaction(t); |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | }, |
---|
205 | |
---|
206 | getCallData: function(t){ |
---|
207 | return { |
---|
208 | action: t.action, |
---|
209 | method: t.method, |
---|
210 | data: t.data, |
---|
211 | type: 'rpc', |
---|
212 | tid: t.tid |
---|
213 | }; |
---|
214 | }, |
---|
215 | |
---|
216 | doSend : function(data){ |
---|
217 | var o = { |
---|
218 | url: this.url, |
---|
219 | callback: this.onData, |
---|
220 | scope: this, |
---|
221 | ts: data |
---|
222 | }; |
---|
223 | |
---|
224 | // send only needed data |
---|
225 | var callData; |
---|
226 | if(Ext.isArray(data)){ |
---|
227 | callData = []; |
---|
228 | for(var i = 0, len = data.length; i < len; i++){ |
---|
229 | callData.push(this.getCallData(data[i])); |
---|
230 | } |
---|
231 | }else{ |
---|
232 | callData = this.getCallData(data); |
---|
233 | } |
---|
234 | |
---|
235 | if(this.enableUrlEncode){ |
---|
236 | var params = {}; |
---|
237 | params[typeof this.enableUrlEncode == 'string' ? this.enableUrlEncode : 'data'] = Ext.encode(callData); |
---|
238 | o.params = params; |
---|
239 | }else{ |
---|
240 | o.jsonData = callData; |
---|
241 | } |
---|
242 | Ext.Ajax.request(o); |
---|
243 | }, |
---|
244 | |
---|
245 | combineAndSend : function(){ |
---|
246 | var len = this.callBuffer.length; |
---|
247 | if(len > 0){ |
---|
248 | this.doSend(len == 1 ? this.callBuffer[0] : this.callBuffer); |
---|
249 | this.callBuffer = []; |
---|
250 | } |
---|
251 | }, |
---|
252 | |
---|
253 | queueTransaction: function(t){ |
---|
254 | if(t.form){ |
---|
255 | this.processForm(t); |
---|
256 | return; |
---|
257 | } |
---|
258 | this.callBuffer.push(t); |
---|
259 | if(this.enableBuffer){ |
---|
260 | if(!this.callTask){ |
---|
261 | this.callTask = new Ext.util.DelayedTask(this.combineAndSend, this); |
---|
262 | } |
---|
263 | this.callTask.delay(typeof this.enableBuffer == 'number' ? this.enableBuffer : 10); |
---|
264 | }else{ |
---|
265 | this.combineAndSend(); |
---|
266 | } |
---|
267 | }, |
---|
268 | |
---|
269 | doCall : function(c, m, args){ |
---|
270 | var data = null, hs = args[m.len], scope = args[m.len+1]; |
---|
271 | |
---|
272 | if(m.len !== 0){ |
---|
273 | data = args.slice(0, m.len); |
---|
274 | } |
---|
275 | |
---|
276 | var t = new Ext.Direct.Transaction({ |
---|
277 | provider: this, |
---|
278 | args: args, |
---|
279 | action: c, |
---|
280 | method: m.name, |
---|
281 | data: data, |
---|
282 | cb: scope && Ext.isFunction(hs) ? hs.createDelegate(scope) : hs |
---|
283 | }); |
---|
284 | |
---|
285 | if(this.fireEvent('beforecall', this, t) !== false){ |
---|
286 | Ext.Direct.addTransaction(t); |
---|
287 | this.queueTransaction(t); |
---|
288 | this.fireEvent('call', this, t); |
---|
289 | } |
---|
290 | }, |
---|
291 | |
---|
292 | doForm : function(c, m, form, callback, scope){ |
---|
293 | var t = new Ext.Direct.Transaction({ |
---|
294 | provider: this, |
---|
295 | action: c, |
---|
296 | method: m.name, |
---|
297 | args:[form, callback, scope], |
---|
298 | cb: scope && Ext.isFunction(callback) ? callback.createDelegate(scope) : callback, |
---|
299 | isForm: true |
---|
300 | }); |
---|
301 | |
---|
302 | if(this.fireEvent('beforecall', this, t) !== false){ |
---|
303 | Ext.Direct.addTransaction(t); |
---|
304 | var isUpload = String(form.getAttribute("enctype")).toLowerCase() == 'multipart/form-data', |
---|
305 | params = { |
---|
306 | extTID: t.tid, |
---|
307 | extAction: c, |
---|
308 | extMethod: m.name, |
---|
309 | extType: 'rpc', |
---|
310 | extUpload: String(isUpload) |
---|
311 | }; |
---|
312 | |
---|
313 | // change made from typeof callback check to callback.params |
---|
314 | // to support addl param passing in DirectSubmit EAC 6/2 |
---|
315 | Ext.apply(t, { |
---|
316 | form: Ext.getDom(form), |
---|
317 | isUpload: isUpload, |
---|
318 | params: callback && Ext.isObject(callback.params) ? Ext.apply(params, callback.params) : params |
---|
319 | }); |
---|
320 | this.fireEvent('call', this, t); |
---|
321 | this.processForm(t); |
---|
322 | } |
---|
323 | }, |
---|
324 | |
---|
325 | processForm: function(t){ |
---|
326 | Ext.Ajax.request({ |
---|
327 | url: this.url, |
---|
328 | params: t.params, |
---|
329 | callback: this.onData, |
---|
330 | scope: this, |
---|
331 | form: t.form, |
---|
332 | isUpload: t.isUpload, |
---|
333 | ts: t |
---|
334 | }); |
---|
335 | }, |
---|
336 | |
---|
337 | createMethod : function(c, m){ |
---|
338 | var f; |
---|
339 | if(!m.formHandler){ |
---|
340 | f = function(){ |
---|
341 | this.doCall(c, m, Array.prototype.slice.call(arguments, 0)); |
---|
342 | }.createDelegate(this); |
---|
343 | }else{ |
---|
344 | f = function(form, callback, scope){ |
---|
345 | this.doForm(c, m, form, callback, scope); |
---|
346 | }.createDelegate(this); |
---|
347 | } |
---|
348 | f.directCfg = { |
---|
349 | action: c, |
---|
350 | method: m |
---|
351 | }; |
---|
352 | return f; |
---|
353 | }, |
---|
354 | |
---|
355 | getTransaction: function(opt){ |
---|
356 | return opt && opt.tid ? Ext.Direct.getTransaction(opt.tid) : null; |
---|
357 | }, |
---|
358 | |
---|
359 | doCallback: function(t, e){ |
---|
360 | var fn = e.status ? 'success' : 'failure'; |
---|
361 | if(t && t.cb){ |
---|
362 | var hs = t.cb; |
---|
363 | var result = e.result || e.data; |
---|
364 | if(Ext.isFunction(hs)){ |
---|
365 | hs(result, e); |
---|
366 | } else{ |
---|
367 | Ext.callback(hs[fn], hs.scope, [result, e]); |
---|
368 | Ext.callback(hs.callback, hs.scope, [result, e]); |
---|
369 | } |
---|
370 | } |
---|
371 | } |
---|
372 | }); |
---|
373 | Ext.Direct.PROVIDERS['remoting'] = Ext.direct.RemotingProvider; |
---|