3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.0
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.direct.RemotingProvider"></div>/**
16 * @class Ext.direct.RemotingProvider
17 * @extends Ext.direct.JsonProvider
19 * <p>The {@link Ext.direct.RemotingProvider RemotingProvider} exposes access to
20 * server side methods on the client (a remote procedure call (RPC) type of
21 * connection where the client can initiate a procedure on the server).</p>
23 * <p>This allows for code to be organized in a fashion that is maintainable,
24 * while providing a clear path between client and server, something that is
25 * not always apparent when using URLs.</p>
27 * <p>To accomplish this the server-side needs to describe what classes and methods
28 * are available on the client-side. This configuration will typically be
29 * outputted by the server-side Ext.Direct stack when the API description is built.</p>
31 Ext.direct.RemotingProvider = Ext.extend(Ext.direct.JsonProvider, {
32 <div id="cfg-Ext.direct.RemotingProvider-actions"></div>/**
33 * @cfg {Object} actions
34 * Object literal defining the server side actions and methods. For example, if
35 * the Provider is configured with:
37 "actions":{ // each property within the 'actions' object represents a server side Class
38 "TestAction":[ // array of methods within each server side Class to be
39 { // stubbed out on client
43 "name":"multiply",// name of method
44 "len":2 // The number of parameters that will be used to create an
45 // array of data to send to the server side function.
46 // Ensure the server sends back a Number, not a String.
49 "formHandler":true, // direct the client to use specialized form handling method
54 * <p>Note that a Store is not required, a server method can be called at any time.
55 * In the following example a <b>client side</b> handler is used to call the
56 * server side method "multiply" in the server-side "TestAction" Class:</p>
59 2, 4, // pass two arguments to server, so specify len=2
60 // callback function after the server is called
61 // result: the result returned by the server
62 // e: Ext.Direct.RemotingEvent object
64 var t = e.getTransaction();
65 var action = t.action; // server side Class called
66 var method = t.method; // server side method called
68 var answer = Ext.encode(result); // 8
71 var msg = e.message; // failure message
76 * In the example above, the server side "multiply" function will be passed two
77 * arguments (2 and 4). The "multiply" method should return the value 8 which will be
78 * available as the <tt>result</tt> in the example above.
81 <div id="cfg-Ext.direct.RemotingProvider-namespace"></div>/**
82 * @cfg {String/Object} namespace
83 * Namespace for the Remoting Provider (defaults to the browser global scope of <i>window</i>).
84 * Explicitly specify the namespace Object, or specify a String to have a
85 * {@link Ext#namespace namespace created} implicitly.
88 <div id="cfg-Ext.direct.RemotingProvider-url"></div>/**
90 * <b>Required<b>. The url to connect to the {@link Ext.Direct} server-side router.
93 <div id="cfg-Ext.direct.RemotingProvider-enableUrlEncode"></div>/**
94 * @cfg {String} enableUrlEncode
95 * Specify which param will hold the arguments for the method.
96 * Defaults to <tt>'data'</tt>.
99 <div id="cfg-Ext.direct.RemotingProvider-enableBuffer"></div>/**
100 * @cfg {Number/Boolean} enableBuffer
101 * <p><tt>true</tt> or <tt>false</tt> to enable or disable combining of method
102 * calls. If a number is specified this is the amount of time in milliseconds
103 * to wait before sending a batched request (defaults to <tt>10</tt>).</p>
104 * <br><p>Calls which are received within the specified timeframe will be
105 * concatenated together and sent in a single request, optimizing the
106 * application by reducing the amount of round trips that have to be made
111 <div id="cfg-Ext.direct.RemotingProvider-maxRetries"></div>/**
112 * @cfg {Number} maxRetries
113 * Number of times to re-attempt delivery on failure of a call. Defaults to <tt>1</tt>.
117 <div id="cfg-Ext.direct.RemotingProvider-timeout"></div>/**
118 * @cfg {Number} timeout
119 * The timeout to use for each request. Defaults to <tt>undefined</tt>.
123 constructor : function(config){
124 Ext.direct.RemotingProvider.superclass.constructor.call(this, config);
126 <div id="event-Ext.direct.RemotingProvider-beforecall"></div>/**
128 * Fires immediately before the client-side sends off the RPC call.
129 * By returning false from an event handler you can prevent the call from
131 * @param {Ext.direct.RemotingProvider} provider
132 * @param {Ext.Direct.Transaction} transaction
133 * @param {Object} meta The meta data
136 <div id="event-Ext.direct.RemotingProvider-call"></div>/**
138 * Fires immediately after the request to the server-side is sent. This does
139 * NOT fire after the response has come back from the call.
140 * @param {Ext.direct.RemotingProvider} provider
141 * @param {Ext.Direct.Transaction} transaction
142 * @param {Object} meta The meta data
146 this.namespace = (Ext.isString(this.namespace)) ? Ext.ns(this.namespace) : this.namespace || window;
147 this.transactions = {};
148 this.callBuffer = [];
152 initAPI : function(){
153 var o = this.actions;
155 var cls = this.namespace[c] || (this.namespace[c] = {}),
157 for(var i = 0, len = ms.length; i < len; i++){
159 cls[m.name] = this.createMethod(c, m);
165 isConnected: function(){
166 return !!this.connected;
172 this.connected = true;
173 this.fireEvent('connect', this);
175 throw 'Error initializing RemotingProvider, no url configured.';
179 disconnect: function(){
181 this.connected = false;
182 this.fireEvent('disconnect', this);
186 onData: function(opt, success, xhr){
188 var events = this.getEvents(xhr);
189 for(var i = 0, len = events.length; i < len; i++){
191 t = this.getTransaction(e);
192 this.fireEvent('data', this, e);
194 this.doCallback(t, e, true);
195 Ext.Direct.removeTransaction(t);
199 var ts = [].concat(opt.ts);
200 for(var i = 0, len = ts.length; i < len; i++){
201 var t = this.getTransaction(ts[i]);
202 if(t && t.retryCount < this.maxRetries){
205 var e = new Ext.Direct.ExceptionEvent({
208 code: Ext.Direct.exceptions.TRANSPORT,
209 message: 'Unable to connect to the server.',
212 this.fireEvent('data', this, e);
214 this.doCallback(t, e, false);
215 Ext.Direct.removeTransaction(t);
222 getCallData: function(t){
232 doSend : function(data){
235 callback: this.onData,
238 timeout: this.timeout
241 if(Ext.isArray(data)){
243 for(var i = 0, len = data.length; i < len; i++){
244 callData.push(this.getCallData(data[i]));
247 callData = this.getCallData(data);
250 if(this.enableUrlEncode){
252 params[Ext.isString(this.enableUrlEncode) ? this.enableUrlEncode : 'data'] = Ext.encode(callData);
255 o.jsonData = callData;
260 combineAndSend : function(){
261 var len = this.callBuffer.length;
263 this.doSend(len == 1 ? this.callBuffer[0] : this.callBuffer);
264 this.callBuffer = [];
268 queueTransaction: function(t){
273 this.callBuffer.push(t);
274 if(this.enableBuffer){
276 this.callTask = new Ext.util.DelayedTask(this.combineAndSend, this);
278 this.callTask.delay(Ext.isNumber(this.enableBuffer) ? this.enableBuffer : 10);
280 this.combineAndSend();
284 doCall : function(c, m, args){
285 var data = null, hs = args[m.len], scope = args[m.len+1];
288 data = args.slice(0, m.len);
291 var t = new Ext.Direct.Transaction({
297 cb: scope && Ext.isFunction(hs) ? hs.createDelegate(scope) : hs
300 if(this.fireEvent('beforecall', this, t, m) !== false){
301 Ext.Direct.addTransaction(t);
302 this.queueTransaction(t);
303 this.fireEvent('call', this, t, m);
307 doForm : function(c, m, form, callback, scope){
308 var t = new Ext.Direct.Transaction({
312 args:[form, callback, scope],
313 cb: scope && Ext.isFunction(callback) ? callback.createDelegate(scope) : callback,
317 if(this.fireEvent('beforecall', this, t, m) !== false){
318 Ext.Direct.addTransaction(t);
319 var isUpload = String(form.getAttribute("enctype")).toLowerCase() == 'multipart/form-data',
325 extUpload: String(isUpload)
328 // change made from typeof callback check to callback.params
329 // to support addl param passing in DirectSubmit EAC 6/2
331 form: Ext.getDom(form),
333 params: callback && Ext.isObject(callback.params) ? Ext.apply(params, callback.params) : params
335 this.fireEvent('call', this, t, m);
340 processForm: function(t){
344 callback: this.onData,
347 isUpload: t.isUpload,
352 createMethod : function(c, m){
356 this.doCall(c, m, Array.prototype.slice.call(arguments, 0));
357 }.createDelegate(this);
359 f = function(form, callback, scope){
360 this.doForm(c, m, form, callback, scope);
361 }.createDelegate(this);
370 getTransaction: function(opt){
371 return opt && opt.tid ? Ext.Direct.getTransaction(opt.tid) : null;
374 doCallback: function(t, e){
375 var fn = e.status ? 'success' : 'failure';
378 result = Ext.isDefined(e.result) ? e.result : e.data;
379 if(Ext.isFunction(hs)){
382 Ext.callback(hs[fn], hs.scope, [result, e]);
383 Ext.callback(hs.callback, hs.scope, [result, e]);
388 Ext.Direct.PROVIDERS['remoting'] = Ext.direct.RemotingProvider;</pre>