4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-direct-RemotingProvider'>/**
19 </span> * @class Ext.direct.RemotingProvider
20 * @extends Ext.direct.JsonProvider
22 * <p>The {@link Ext.direct.RemotingProvider RemotingProvider} exposes access to
23 * server side methods on the client (a remote procedure call (RPC) type of
24 * connection where the client can initiate a procedure on the server).</p>
26 * <p>This allows for code to be organized in a fashion that is maintainable,
27 * while providing a clear path between client and server, something that is
28 * not always apparent when using URLs.</p>
30 * <p>To accomplish this the server-side needs to describe what classes and methods
31 * are available on the client-side. This configuration will typically be
32 * outputted by the server-side Ext.Direct stack when the API description is built.</p>
34 Ext.define('Ext.direct.RemotingProvider', {
36 /* Begin Definitions */
38 alias: 'direct.remotingprovider',
40 extend: 'Ext.direct.JsonProvider',
43 'Ext.util.MixedCollection',
44 'Ext.util.DelayedTask',
45 'Ext.direct.Transaction',
46 'Ext.direct.RemotingMethod'
51 <span id='Ext-direct-RemotingProvider-cfg-actions'> /**
52 </span> * @cfg {Object} actions
53 * Object literal defining the server side actions and methods. For example, if
54 * the Provider is configured with:
55 * <pre><code>
56 "actions":{ // each property within the 'actions' object represents a server side Class
57 "TestAction":[ // array of methods within each server side Class to be
58 { // stubbed out on client
59 "name":"doEcho",
62 "name":"multiply",// name of method
63 "len":2 // The number of parameters that will be used to create an
64 // array of data to send to the server side function.
65 // Ensure the server sends back a Number, not a String.
67 "name":"doForm",
68 "formHandler":true, // direct the client to use specialized form handling method
72 * </code></pre>
73 * <p>Note that a Store is not required, a server method can be called at any time.
74 * In the following example a <b>client side</b> handler is used to call the
75 * server side method "multiply" in the server-side "TestAction" Class:</p>
76 * <pre><code>
78 2, 4, // pass two arguments to server, so specify len=2
79 // callback function after the server is called
80 // result: the result returned by the server
81 // e: Ext.direct.RemotingEvent object
83 var t = e.getTransaction();
84 var action = t.action; // server side Class called
85 var method = t.method; // server side method called
87 var answer = Ext.encode(result); // 8
90 var msg = e.message; // failure message
94 * </code></pre>
95 * In the example above, the server side "multiply" function will be passed two
96 * arguments (2 and 4). The "multiply" method should return the value 8 which will be
97 * available as the <tt>result</tt> in the example above.
100 <span id='Ext-direct-RemotingProvider-cfg-namespace'> /**
101 </span> * @cfg {String/Object} namespace
102 * Namespace for the Remoting Provider (defaults to the browser global scope of <i>window</i>).
103 * Explicitly specify the namespace Object, or specify a String to have a
104 * {@link Ext#namespace namespace created} implicitly.
107 <span id='Ext-direct-RemotingProvider-cfg-url'> /**
108 </span> * @cfg {String} url
109 * <b>Required</b>. The url to connect to the {@link Ext.direct.Manager} server-side router.
112 <span id='Ext-direct-RemotingProvider-cfg-enableUrlEncode'> /**
113 </span> * @cfg {String} enableUrlEncode
114 * Specify which param will hold the arguments for the method.
115 * Defaults to <tt>'data'</tt>.
118 <span id='Ext-direct-RemotingProvider-cfg-enableBuffer'> /**
119 </span> * @cfg {Number/Boolean} enableBuffer
120 * <p><tt>true</tt> or <tt>false</tt> to enable or disable combining of method
121 * calls. If a number is specified this is the amount of time in milliseconds
122 * to wait before sending a batched request (defaults to <tt>10</tt>).</p>
123 * <br><p>Calls which are received within the specified timeframe will be
124 * concatenated together and sent in a single request, optimizing the
125 * application by reducing the amount of round trips that have to be made
126 * to the server.</p>
130 <span id='Ext-direct-RemotingProvider-cfg-maxRetries'> /**
131 </span> * @cfg {Number} maxRetries
132 * Number of times to re-attempt delivery on failure of a call. Defaults to <tt>1</tt>.
136 <span id='Ext-direct-RemotingProvider-cfg-timeout'> /**
137 </span> * @cfg {Number} timeout
138 * The timeout to use for each request. Defaults to <tt>undefined</tt>.
142 constructor : function(config){
144 me.callParent(arguments);
146 <span id='Ext-direct-RemotingProvider-event-beforecall'> /**
147 </span> * @event beforecall
148 * Fires immediately before the client-side sends off the RPC call.
149 * By returning false from an event handler you can prevent the call from
151 * @param {Ext.direct.RemotingProvider} provider
152 * @param {Ext.direct.Transaction} transaction
153 * @param {Object} meta The meta data
156 <span id='Ext-direct-RemotingProvider-event-call'> /**
157 </span> * @event call
158 * Fires immediately after the request to the server-side is sent. This does
159 * NOT fire after the response has come back from the call.
160 * @param {Ext.direct.RemotingProvider} provider
161 * @param {Ext.direct.Transaction} transaction
162 * @param {Object} meta The meta data
166 me.namespace = (Ext.isString(me.namespace)) ? Ext.ns(me.namespace) : me.namespace || window;
167 me.transactions = Ext.create('Ext.util.MixedCollection');
171 <span id='Ext-direct-RemotingProvider-method-initAPI'> /**
172 </span> * Initialize the API
175 initAPI : function(){
176 var actions = this.actions,
177 namespace = this.namespace,
185 for (action in actions) {
186 cls = namespace[action];
188 cls = namespace[action] = {};
190 methods = actions[action];
192 for (i = 0, len = methods.length; i < len; ++i) {
193 method = Ext.create('Ext.direct.RemotingMethod', methods[i]);
194 cls[method.name] = this.createHandler(action, method);
199 <span id='Ext-direct-RemotingProvider-method-createHandler'> /**
200 </span> * Create a handler function for a direct call.
202 * @param {String} action The action the call is for
203 * @param {Object} method The details of the method
204 * @return {Function} A JS function that will kick off the call
206 createHandler : function(action, method){
210 if (!method.formHandler) {
211 handler = function(){
212 me.configureRequest(action, method, Array.prototype.slice.call(arguments, 0));
215 handler = function(form, callback, scope){
216 me.configureFormRequest(action, method, form, callback, scope);
219 handler.directCfg = {
227 isConnected: function(){
228 return !!this.connected;
238 me.fireEvent('connect', me);
241 Ext.Error.raise('Error initializing RemotingProvider, no url configured.');
247 disconnect: function(){
251 me.connected = false;
252 me.fireEvent('disconnect', me);
256 <span id='Ext-direct-RemotingProvider-method-runCallback'> /**
257 </span> * Run any callbacks related to the transaction.
259 * @param {Ext.direct.Transaction} transaction The transaction
260 * @param {Ext.direct.Event} event The event
262 runCallback: function(transaction, event){
263 var funcName = event.status ? 'success' : 'failure',
267 if (transaction && transaction.callback) {
268 callback = transaction.callback;
269 result = Ext.isDefined(event.result) ? event.result : event.data;
271 if (Ext.isFunction(callback)) {
272 callback(result, event);
274 Ext.callback(callback[funcName], callback.scope, [result, event]);
275 Ext.callback(callback.callback, callback.scope, [result, event]);
280 <span id='Ext-direct-RemotingProvider-method-onData'> /**
281 </span> * React to the ajax request being completed
284 onData: function(options, success, response){
294 events = me.createEvents(response);
295 for (len = events.length; i < len; ++i) {
297 transaction = me.getTransaction(event);
298 me.fireEvent('data', me, event);
300 me.runCallback(transaction, event, true);
301 Ext.direct.Manager.removeTransaction(transaction);
305 transactions = [].concat(options.transaction);
306 for (len = transactions.length; i < len; ++i) {
307 transaction = me.getTransaction(transactions[i]);
308 if (transaction && transaction.retryCount < me.maxRetries) {
311 event = Ext.create('Ext.direct.ExceptionEvent', {
313 transaction: transaction,
314 code: Ext.direct.Manager.self.exceptions.TRANSPORT,
315 message: 'Unable to connect to the server.',
318 me.fireEvent('data', me, event);
320 me.runCallback(transaction, event, false);
321 Ext.direct.Manager.removeTransaction(transaction);
328 <span id='Ext-direct-RemotingProvider-method-getTransaction'> /**
329 </span> * Get transaction from XHR options
331 * @param {Object} options The options sent to the Ajax request
332 * @return {Ext.direct.Transaction} The transaction, null if not found
334 getTransaction: function(options){
335 return options && options.tid ? Ext.direct.Manager.getTransaction(options.tid) : null;
338 <span id='Ext-direct-RemotingProvider-method-configureRequest'> /**
339 </span> * Configure a direct request
341 * @param {String} action The action being executed
342 * @param {Object} method The being executed
344 configureRequest: function(action, method, args){
346 callData = method.getCallData(args),
347 data = callData.data,
348 callback = callData.callback,
349 scope = callData.scope,
352 transaction = Ext.create('Ext.direct.Transaction', {
358 callback: scope && Ext.isFunction(callback) ? Ext.Function.bind(callback, scope) : callback
361 if (me.fireEvent('beforecall', me, transaction, method) !== false) {
362 Ext.direct.Manager.addTransaction(transaction);
363 me.queueTransaction(transaction);
364 me.fireEvent('call', me, transaction, method);
368 <span id='Ext-direct-RemotingProvider-method-getCallData'> /**
369 </span> * Gets the Ajax call info for a transaction
371 * @param {Ext.direct.Transaction} transaction The transaction
372 * @return {Object} The call params
374 getCallData: function(transaction){
376 action: transaction.action,
377 method: transaction.method,
378 data: transaction.data,
384 <span id='Ext-direct-RemotingProvider-method-sendRequest'> /**
385 </span> * Sends a request to the server
387 * @param {Object/Array} data The data to send
389 sendRequest : function(data){
398 enableUrlEncode = me.enableUrlEncode,
404 if (Ext.isArray(data)) {
406 for (len = data.length; i < len; ++i) {
407 callData.push(me.getCallData(data[i]));
410 callData = me.getCallData(data);
413 if (enableUrlEncode) {
415 params[Ext.isString(enableUrlEncode) ? enableUrlEncode : 'data'] = Ext.encode(callData);
416 request.params = params;
418 request.jsonData = callData;
420 Ext.Ajax.request(request);
423 <span id='Ext-direct-RemotingProvider-method-queueTransaction'> /**
424 </span> * Add a new transaction to the queue
426 * @param {Ext.direct.Transaction} transaction The transaction
428 queueTransaction: function(transaction){
430 enableBuffer = me.enableBuffer;
432 if (transaction.form) {
433 me.sendFormRequest(transaction);
437 me.callBuffer.push(transaction);
440 me.callTask = Ext.create('Ext.util.DelayedTask', me.combineAndSend, me);
442 me.callTask.delay(Ext.isNumber(enableBuffer) ? enableBuffer : 10);
448 <span id='Ext-direct-RemotingProvider-method-combineAndSend'> /**
449 </span> * Combine any buffered requests and send them off
452 combineAndSend : function(){
453 var buffer = this.callBuffer,
457 this.sendRequest(len == 1 ? buffer[0] : buffer);
458 this.callBuffer = [];
462 <span id='Ext-direct-RemotingProvider-method-configureFormRequest'> /**
463 </span> * Configure a form submission request
465 * @param {String} action The action being executed
466 * @param {Object} method The method being executed
467 * @param {HTMLElement} form The form being submitted
468 * @param {Function} callback (optional) A callback to run after the form submits
469 * @param {Object} scope A scope to execute the callback in
471 configureFormRequest : function(action, method, form, callback, scope){
473 transaction = Ext.create('Ext.direct.Transaction', {
477 args: [form, callback, scope],
478 callback: scope && Ext.isFunction(callback) ? Ext.Function.bind(callback, scope) : callback,
484 if (me.fireEvent('beforecall', me, transaction, method) !== false) {
485 Ext.direct.Manager.addTransaction(transaction);
486 isUpload = String(form.getAttribute("enctype")).toLowerCase() == 'multipart/form-data';
489 extTID: transaction.id,
491 extMethod: method.name,
493 extUpload: String(isUpload)
496 // change made from typeof callback check to callback.params
497 // to support addl param passing in DirectSubmit EAC 6/2
498 Ext.apply(transaction, {
499 form: Ext.getDom(form),
501 params: callback && Ext.isObject(callback.params) ? Ext.apply(params, callback.params) : params
503 me.fireEvent('call', me, transaction, method);
504 me.sendFormRequest(transaction);
508 <span id='Ext-direct-RemotingProvider-method-sendFormRequest'> /**
509 </span> * Sends a form request
511 * @param {Ext.direct.Transaction} transaction The transaction to send
513 sendFormRequest: function(transaction){
516 params: transaction.params,
517 callback: this.onData,
519 form: transaction.form,
520 isUpload: transaction.isUpload,
521 transaction: transaction