/hippo/src/main/webapp/ext/src/data/DirectProxy.js

http://hdbc.googlecode.com/ · JavaScript · 141 lines · 81 code · 10 blank · 50 comment · 12 complexity · 40b5e23ef17047c1b5253f43d7669b40 MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.0.0
  3. * Copyright(c) 2006-2009 Ext JS, LLC
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /**
  8. * @class Ext.data.DirectProxy
  9. * @extends Ext.data.DataProxy
  10. */
  11. Ext.data.DirectProxy = function(config){
  12. Ext.apply(this, config);
  13. if(typeof this.paramOrder == 'string'){
  14. this.paramOrder = this.paramOrder.split(/[\s,|]/);
  15. }
  16. Ext.data.DirectProxy.superclass.constructor.call(this, config);
  17. };
  18. Ext.extend(Ext.data.DirectProxy, Ext.data.DataProxy, {
  19. /**
  20. * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. A list of params to be executed
  21. * server side. Specify the params in the order in which they must be executed on the server-side
  22. * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,
  23. * comma, or pipe. For example,
  24. * any of the following would be acceptable:<pre><code>
  25. paramOrder: ['param1','param2','param3']
  26. paramOrder: 'param1 param2 param3'
  27. paramOrder: 'param1,param2,param3'
  28. paramOrder: 'param1|param2|param'
  29. </code></pre>
  30. */
  31. paramOrder: undefined,
  32. /**
  33. * @cfg {Boolean} paramsAsHash
  34. * Send parameters as a collection of named arguments (defaults to <tt>true</tt>). Providing a
  35. * <tt>{@link #paramOrder}</tt> nullifies this configuration.
  36. */
  37. paramsAsHash: true,
  38. /**
  39. * @cfg {Function} directFn
  40. * Function to call when executing a request. directFn is a simple alternative to defining the api configuration-parameter
  41. * for Store's which will not implement a full CRUD api.
  42. */
  43. directFn : undefined,
  44. // protected
  45. doRequest : function(action, rs, params, reader, callback, scope, options) {
  46. var args = [];
  47. var directFn = this.api[action] || this.directFn;
  48. switch (action) {
  49. case Ext.data.Api.actions.create:
  50. args.push(params[reader.meta.root]); // <-- create(Hash)
  51. break;
  52. case Ext.data.Api.actions.read:
  53. if(this.paramOrder){
  54. for(var i = 0, len = this.paramOrder.length; i < len; i++){
  55. args.push(params[this.paramOrder[i]]);
  56. }
  57. }else if(this.paramsAsHash){
  58. args.push(params);
  59. }
  60. break;
  61. case Ext.data.Api.actions.update:
  62. args.push(params[reader.meta.idProperty]); // <-- save(Integer/Integer[], Hash/Hash[])
  63. args.push(params[reader.meta.root]);
  64. break;
  65. case Ext.data.Api.actions.destroy:
  66. args.push(params[reader.meta.root]); // <-- destroy(Int/Int[])
  67. break;
  68. }
  69. var trans = {
  70. params : params || {},
  71. callback : callback,
  72. scope : scope,
  73. arg : options,
  74. reader: reader
  75. };
  76. args.push(this.createCallback(action, rs, trans), this);
  77. directFn.apply(window, args);
  78. },
  79. // private
  80. createCallback : function(action, rs, trans) {
  81. return function(result, res) {
  82. if (!res.status) {
  83. // @deprecated fire loadexception
  84. if (action === Ext.data.Api.actions.read) {
  85. this.fireEvent("loadexception", this, trans, res, null);
  86. }
  87. this.fireEvent('exception', this, 'remote', action, trans, res, null);
  88. trans.callback.call(trans.scope, null, trans.arg, false);
  89. return;
  90. }
  91. if (action === Ext.data.Api.actions.read) {
  92. this.onRead(action, trans, result, res);
  93. } else {
  94. this.onWrite(action, trans, result, res, rs);
  95. }
  96. };
  97. },
  98. /**
  99. * Callback for read actions
  100. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  101. * @param {Object} trans The request transaction object
  102. * @param {Object} res The server response
  103. * @private
  104. */
  105. onRead : function(action, trans, result, res) {
  106. var records;
  107. try {
  108. records = trans.reader.readRecords(result);
  109. }
  110. catch (ex) {
  111. // @deprecated: Fire old loadexception for backwards-compat.
  112. this.fireEvent("loadexception", this, trans, res, ex);
  113. this.fireEvent('exception', this, 'response', action, trans, res, ex);
  114. trans.callback.call(trans.scope, null, trans.arg, false);
  115. return;
  116. }
  117. this.fireEvent("load", this, res, trans.arg);
  118. trans.callback.call(trans.scope, records, trans.arg, true);
  119. },
  120. /**
  121. * Callback for write actions
  122. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  123. * @param {Object} trans The request transaction object
  124. * @param {Object} res The server response
  125. * @private
  126. */
  127. onWrite : function(action, trans, result, res, rs) {
  128. this.fireEvent("write", this, action, result, res, rs, trans.arg);
  129. trans.callback.call(trans.scope, result, res, true);
  130. }
  131. });