You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

393 lines
15 KiB

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