Direct.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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-data-proxy-Direct'>/**
  19. </span> * This class is used to send requests to the server using {@link Ext.direct.Manager Ext.Direct}. When a
  20. * request is made, the transport mechanism is handed off to the appropriate
  21. * {@link Ext.direct.RemotingProvider Provider} to complete the call.
  22. *
  23. * # Specifying the function
  24. *
  25. * This proxy expects a Direct remoting method to be passed in order to be able to complete requests.
  26. * This can be done by specifying the {@link #directFn} configuration. This will use the same direct
  27. * method for all requests. Alternatively, you can provide an {@link #api} configuration. This
  28. * allows you to specify a different remoting method for each CRUD action.
  29. *
  30. * # Parameters
  31. *
  32. * This proxy provides options to help configure which parameters will be sent to the server.
  33. * By specifying the {@link #paramsAsHash} option, it will send an object literal containing each
  34. * of the passed parameters. The {@link #paramOrder} option can be used to specify the order in which
  35. * the remoting method parameters are passed.
  36. *
  37. * # Example Usage
  38. *
  39. * Ext.define('User', {
  40. * extend: 'Ext.data.Model',
  41. * fields: ['firstName', 'lastName'],
  42. * proxy: {
  43. * type: 'direct',
  44. * directFn: MyApp.getUsers,
  45. * paramOrder: 'id' // Tells the proxy to pass the id as the first parameter to the remoting method.
  46. * }
  47. * });
  48. * User.load(1);
  49. */
  50. Ext.define('Ext.data.proxy.Direct', {
  51. /* Begin Definitions */
  52. extend: 'Ext.data.proxy.Server',
  53. alternateClassName: 'Ext.data.DirectProxy',
  54. alias: 'proxy.direct',
  55. requires: ['Ext.direct.Manager'],
  56. /* End Definitions */
  57. <span id='Ext-data-proxy-Direct-cfg-paramOrder'> /**
  58. </span> * @cfg {String/String[]} paramOrder
  59. * Defaults to undefined. A list of params to be executed server side. Specify the params in the order in
  60. * which they must be executed on the server-side as either (1) an Array of String values, or (2) a String
  61. * of params delimited by either whitespace, comma, or pipe. For example, any of the following would be
  62. * acceptable:
  63. *
  64. * paramOrder: ['param1','param2','param3']
  65. * paramOrder: 'param1 param2 param3'
  66. * paramOrder: 'param1,param2,param3'
  67. * paramOrder: 'param1|param2|param'
  68. */
  69. paramOrder: undefined,
  70. <span id='Ext-data-proxy-Direct-cfg-paramsAsHash'> /**
  71. </span> * @cfg {Boolean} paramsAsHash
  72. * Send parameters as a collection of named arguments.
  73. * Providing a {@link #paramOrder} nullifies this configuration.
  74. */
  75. paramsAsHash: true,
  76. <span id='Ext-data-proxy-Direct-cfg-directFn'> /**
  77. </span> * @cfg {Function/String} directFn
  78. * Function to call when executing a request. directFn is a simple alternative to defining the api configuration-parameter
  79. * for Store's which will not implement a full CRUD api. The directFn may also be a string reference to the fully qualified
  80. * name of the function, for example: 'MyApp.company.GetProfile'. This can be useful when using dynamic loading. The string
  81. * will be looked up when the proxy is created.
  82. */
  83. directFn : undefined,
  84. <span id='Ext-data-proxy-Direct-cfg-api'> /**
  85. </span> * @cfg {Object} api
  86. * The same as {@link Ext.data.proxy.Server#api}, however instead of providing urls, you should provide a direct
  87. * function call. See {@link #directFn}.
  88. */
  89. <span id='Ext-data-proxy-Direct-cfg-extraParams'> /**
  90. </span> * @cfg {Object} extraParams
  91. * Extra parameters that will be included on every read request. Individual requests with params
  92. * of the same name will override these params when they are in conflict.
  93. */
  94. // private
  95. paramOrderRe: /[\s,|]/,
  96. constructor: function(config){
  97. var me = this,
  98. paramOrder,
  99. fn,
  100. api;
  101. me.callParent(arguments);
  102. paramOrder = me.paramOrder;
  103. if (Ext.isString(paramOrder)) {
  104. me.paramOrder = paramOrder.split(me.paramOrderRe);
  105. }
  106. fn = me.directFn;
  107. if (fn) {
  108. me.directFn = Ext.direct.Manager.parseMethod(fn);
  109. }
  110. api = me.api;
  111. for (fn in api) {
  112. if (api.hasOwnProperty(fn)) {
  113. api[fn] = Ext.direct.Manager.parseMethod(api[fn]);
  114. }
  115. }
  116. },
  117. doRequest: function(operation, callback, scope) {
  118. var me = this,
  119. writer = me.getWriter(),
  120. request = me.buildRequest(operation, callback, scope),
  121. fn = me.api[request.action] || me.directFn,
  122. params = request.params,
  123. args = [],
  124. method;
  125. //&lt;debug&gt;
  126. if (!fn) {
  127. Ext.Error.raise('No direct function specified for this proxy');
  128. }
  129. //&lt;/debug&gt;
  130. if (operation.allowWrite()) {
  131. request = writer.write(request);
  132. }
  133. if (operation.action == 'read') {
  134. // We need to pass params
  135. method = fn.directCfg.method;
  136. args = method.getArgs(params, me.paramOrder, me.paramsAsHash);
  137. } else {
  138. args.push(request.jsonData);
  139. }
  140. Ext.apply(request, {
  141. args: args,
  142. directFn: fn
  143. });
  144. args.push(me.createRequestCallback(request, operation, callback, scope), me);
  145. fn.apply(window, args);
  146. },
  147. /*
  148. * Inherit docs. We don't apply any encoding here because
  149. * all of the direct requests go out as jsonData
  150. */
  151. applyEncoding: function(value){
  152. return value;
  153. },
  154. createRequestCallback: function(request, operation, callback, scope){
  155. var me = this;
  156. return function(data, event){
  157. me.processResponse(event.status, operation, request, event, callback, scope);
  158. };
  159. },
  160. // inherit docs
  161. extractResponseData: function(response){
  162. return Ext.isDefined(response.result) ? response.result : response.data;
  163. },
  164. // inherit docs
  165. setException: function(operation, response) {
  166. operation.setException(response.message);
  167. },
  168. // inherit docs
  169. buildUrl: function(){
  170. return '';
  171. }
  172. });
  173. </pre>
  174. </body>
  175. </html>