/ext-4.1.0_b3/src/data/proxy/Direct.js

https://bitbucket.org/srogerf/javascript · JavaScript · 181 lines · 76 code · 26 blank · 79 comment · 11 complexity · 2f30e33771ea645c2c7ebcebbf5a60ac MD5 · raw file

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