3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.direct.RemotingProvider"></div>/**
\r
9 * @class Ext.direct.RemotingProvider
\r
10 * @extends Ext.direct.JsonProvider
\r
12 * <p>The {@link Ext.direct.RemotingProvider RemotingProvider} exposes access to
\r
13 * server side methods on the client (a remote procedure call (RPC) type of
\r
14 * connection where the client can initiate a procedure on the server).</p>
\r
16 * <p>This allows for code to be organized in a fashion that is maintainable,
\r
17 * while providing a clear path between client and server, something that is
\r
18 * not always apparent when using URLs.</p>
\r
20 * <p>To accomplish this the server-side needs to describe what classes and methods
\r
21 * are available on the client-side. This configuration will typically be
\r
22 * outputted by the server-side Ext.Direct stack when the API description is built.</p>
\r
24 Ext.direct.RemotingProvider = Ext.extend(Ext.direct.JsonProvider, {
\r
25 <div id="cfg-Ext.direct.RemotingProvider-actions"></div>/**
\r
26 * @cfg {Object} actions
\r
27 * Object literal defining the server side actions and methods. For example, if
\r
28 * the Provider is configured with:
\r
30 "actions":{ // each property within the 'actions' object represents a server side Class
\r
31 "TestAction":[ // array of methods within each server side Class to be
\r
32 { // stubbed out on client
\r
36 "name":"multiply",// name of method
\r
37 "len":2 // The number of parameters that will be used to create an
\r
38 // array of data to send to the server side function.
\r
39 // Ensure the server sends back a Number, not a String.
\r
42 "formHandler":true, // direct the client to use specialized form handling method
\r
47 * <p>Note that a Store is not required, a server method can be called at any time.
\r
48 * In the following example a <b>client side</b> handler is used to call the
\r
49 * server side method "multiply" in the server-side "TestAction" Class:</p>
\r
51 TestAction.multiply(
\r
52 2, 4, // pass two arguments to server, so specify len=2
\r
53 // callback function after the server is called
\r
54 // result: the result returned by the server
\r
55 // e: Ext.Direct.RemotingEvent object
\r
56 function(result, e){
\r
57 var t = e.getTransaction();
\r
58 var action = t.action; // server side Class called
\r
59 var method = t.method; // server side method called
\r
61 var answer = Ext.encode(result); // 8
\r
64 var msg = e.message; // failure message
\r
69 * In the example above, the server side "multiply" function will be passed two
\r
70 * arguments (2 and 4). The "multiply" method should return the value 8 which will be
\r
71 * available as the <tt>result</tt> in the example above.
\r
74 <div id="cfg-Ext.direct.RemotingProvider-namespace"></div>/**
\r
75 * @cfg {String/Object} namespace
\r
76 * Namespace for the Remoting Provider (defaults to the browser global scope of <i>window</i>).
\r
77 * Explicitly specify the namespace Object, or specify a String to have a
\r
78 * {@link Ext#namespace namespace created} implicitly.
\r
81 <div id="cfg-Ext.direct.RemotingProvider-url"></div>/**
\r
83 * <b>Required<b>. The url to connect to the {@link Ext.Direct} server-side router.
\r
86 <div id="cfg-Ext.direct.RemotingProvider-enableUrlEncode"></div>/**
\r
87 * @cfg {String} enableUrlEncode
\r
88 * Specify which param will hold the arguments for the method.
\r
89 * Defaults to <tt>'data'</tt>.
\r
92 <div id="cfg-Ext.direct.RemotingProvider-enableBuffer"></div>/**
\r
93 * @cfg {Number/Boolean} enableBuffer
\r
94 * <p><tt>true</tt> or <tt>false</tt> to enable or disable combining of method
\r
95 * calls. If a number is specified this is the amount of time in milliseconds
\r
96 * to wait before sending a batched request (defaults to <tt>10</tt>).</p>
\r
97 * <br><p>Calls which are received within the specified timeframe will be
\r
98 * concatenated together and sent in a single request, optimizing the
\r
99 * application by reducing the amount of round trips that have to be made
\r
100 * to the server.</p>
\r
104 <div id="cfg-Ext.direct.RemotingProvider-maxRetries"></div>/**
\r
105 * @cfg {Number} maxRetries
\r
106 * Number of times to re-attempt delivery on failure of a call.
\r
110 constructor : function(config){
\r
111 Ext.direct.RemotingProvider.superclass.constructor.call(this, config);
\r
113 <div id="event-Ext.direct.RemotingProvider-beforecall"></div>/**
\r
114 * @event beforecall
\r
115 * Fires immediately before the client-side sends off the RPC call.
\r
116 * By returning false from an event handler you can prevent the call from
\r
118 * @param {Ext.direct.RemotingProvider} provider
\r
119 * @param {Ext.Direct.Transaction} transaction
\r
122 <div id="event-Ext.direct.RemotingProvider-call"></div>/**
\r
124 * Fires immediately after the request to the server-side is sent. This does
\r
125 * NOT fire after the response has come back from the call.
\r
126 * @param {Ext.direct.RemotingProvider} provider
\r
127 * @param {Ext.Direct.Transaction} transaction
\r
131 this.namespace = (typeof this.namespace === 'string') ? Ext.ns(this.namespace) : this.namespace || window;
\r
132 this.transactions = {};
\r
133 this.callBuffer = [];
\r
137 initAPI : function(){
\r
138 var o = this.actions;
\r
140 var cls = this.namespace[c] || (this.namespace[c] = {});
\r
142 for(var i = 0, len = ms.length; i < len; i++){
\r
144 cls[m.name] = this.createMethod(c, m);
\r
150 isConnected: function(){
\r
151 return !!this.connected;
\r
154 connect: function(){
\r
157 this.connected = true;
\r
158 this.fireEvent('connect', this);
\r
159 }else if(!this.url){
\r
160 throw 'Error initializing RemotingProvider, no url configured.';
\r
164 disconnect: function(){
\r
165 if(this.connected){
\r
166 this.connected = false;
\r
167 this.fireEvent('disconnect', this);
\r
171 onData: function(opt, success, xhr){
\r
173 var events = this.getEvents(xhr);
\r
174 for(var i = 0, len = events.length; i < len; i++){
\r
176 var t = this.getTransaction(e);
\r
177 this.fireEvent('data', this, e);
\r
179 this.doCallback(t, e, true);
\r
180 Ext.Direct.removeTransaction(t);
\r
184 var ts = [].concat(opt.ts);
\r
185 for(var i = 0, len = ts.length; i < len; i++){
\r
186 var t = this.getTransaction(ts[i]);
\r
187 if(t && t.retryCount < this.maxRetries){
\r
190 var e = new Ext.Direct.ExceptionEvent({
\r
193 code: Ext.Direct.exceptions.TRANSPORT,
\r
194 message: 'Unable to connect to the server.',
\r
197 this.fireEvent('data', this, e);
\r
199 this.doCallback(t, e, false);
\r
200 Ext.Direct.removeTransaction(t);
\r
207 getCallData: function(t){
\r
217 doSend : function(data){
\r
220 callback: this.onData,
\r
225 // send only needed data
\r
227 if(Ext.isArray(data)){
\r
229 for(var i = 0, len = data.length; i < len; i++){
\r
230 callData.push(this.getCallData(data[i]));
\r
233 callData = this.getCallData(data);
\r
236 if(this.enableUrlEncode){
\r
238 params[typeof this.enableUrlEncode == 'string' ? this.enableUrlEncode : 'data'] = Ext.encode(callData);
\r
241 o.jsonData = callData;
\r
243 Ext.Ajax.request(o);
\r
246 combineAndSend : function(){
\r
247 var len = this.callBuffer.length;
\r
249 this.doSend(len == 1 ? this.callBuffer[0] : this.callBuffer);
\r
250 this.callBuffer = [];
\r
254 queueTransaction: function(t){
\r
256 this.processForm(t);
\r
259 this.callBuffer.push(t);
\r
260 if(this.enableBuffer){
\r
261 if(!this.callTask){
\r
262 this.callTask = new Ext.util.DelayedTask(this.combineAndSend, this);
\r
264 this.callTask.delay(typeof this.enableBuffer == 'number' ? this.enableBuffer : 10);
\r
266 this.combineAndSend();
\r
270 doCall : function(c, m, args){
\r
271 var data = null, hs = args[m.len], scope = args[m.len+1];
\r
274 data = args.slice(0, m.len);
\r
277 var t = new Ext.Direct.Transaction({
\r
283 cb: scope && Ext.isFunction(hs) ? hs.createDelegate(scope) : hs
\r
286 if(this.fireEvent('beforecall', this, t) !== false){
\r
287 Ext.Direct.addTransaction(t);
\r
288 this.queueTransaction(t);
\r
289 this.fireEvent('call', this, t);
\r
293 doForm : function(c, m, form, callback, scope){
\r
294 var t = new Ext.Direct.Transaction({
\r
298 args:[form, callback, scope],
\r
299 cb: scope && Ext.isFunction(callback) ? callback.createDelegate(scope) : callback,
\r
303 if(this.fireEvent('beforecall', this, t) !== false){
\r
304 Ext.Direct.addTransaction(t);
\r
305 var isUpload = String(form.getAttribute("enctype")).toLowerCase() == 'multipart/form-data',
\r
311 extUpload: String(isUpload)
\r
314 // change made from typeof callback check to callback.params
\r
315 // to support addl param passing in DirectSubmit EAC 6/2
\r
317 form: Ext.getDom(form),
\r
318 isUpload: isUpload,
\r
319 params: callback && Ext.isObject(callback.params) ? Ext.apply(params, callback.params) : params
\r
321 this.fireEvent('call', this, t);
\r
322 this.processForm(t);
\r
326 processForm: function(t){
\r
330 callback: this.onData,
\r
333 isUpload: t.isUpload,
\r
338 createMethod : function(c, m){
\r
340 if(!m.formHandler){
\r
342 this.doCall(c, m, Array.prototype.slice.call(arguments, 0));
\r
343 }.createDelegate(this);
\r
345 f = function(form, callback, scope){
\r
346 this.doForm(c, m, form, callback, scope);
\r
347 }.createDelegate(this);
\r
356 getTransaction: function(opt){
\r
357 return opt && opt.tid ? Ext.Direct.getTransaction(opt.tid) : null;
\r
360 doCallback: function(t, e){
\r
361 var fn = e.status ? 'success' : 'failure';
\r
364 var result = e.result || e.data;
\r
365 if(Ext.isFunction(hs)){
\r
368 Ext.callback(hs[fn], hs.scope, [result, e]);
\r
369 Ext.callback(hs.callback, hs.scope, [result, e]);
\r
374 Ext.Direct.PROVIDERS['remoting'] = Ext.direct.RemotingProvider;</pre>
\r