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

http://hdbc.googlecode.com/ · JavaScript · 210 lines · 87 code · 10 blank · 113 comment · 17 complexity · 3158f7b88929528b125eb30fea2ba445 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.Api
  9. * @extends Object
  10. * Ext.data.Api is a singleton designed to manage the data API including methods
  11. * for validating a developer's DataProxy API. Defines variables for CRUD actions
  12. * create, read, update and destroy in addition to a mapping of RESTful HTTP methods
  13. * GET, POST, PUT and DELETE to CRUD actions.
  14. * @singleton
  15. */
  16. Ext.data.Api = (function() {
  17. // private validActions. validActions is essentially an inverted hash of Ext.data.Api.actions, where value becomes the key.
  18. // Some methods in this singleton (e.g.: getActions, getVerb) will loop through actions with the code <code>for (var verb in this.actions)</code>
  19. // For efficiency, some methods will first check this hash for a match. Those methods which do acces validActions will cache their result here.
  20. // We cannot pre-define this hash since the developer may over-ride the actions at runtime.
  21. var validActions = {};
  22. return {
  23. /**
  24. * Defined actions corresponding to remote actions:
  25. * <pre><code>
  26. actions: {
  27. create : 'create', // Text representing the remote-action to create records on server.
  28. read : 'read', // Text representing the remote-action to read/load data from server.
  29. update : 'update', // Text representing the remote-action to update records on server.
  30. destroy : 'destroy' // Text representing the remote-action to destroy records on server.
  31. }
  32. * </code></pre>
  33. * @property actions
  34. * @type Object
  35. */
  36. actions : {
  37. create : 'create',
  38. read : 'read',
  39. update : 'update',
  40. destroy : 'destroy'
  41. },
  42. /**
  43. * Defined {CRUD action}:{HTTP method} pairs to associate HTTP methods with the
  44. * corresponding actions for {@link Ext.data.DataProxy#restful RESTful proxies}.
  45. * Defaults to:
  46. * <pre><code>
  47. restActions : {
  48. create : 'POST',
  49. read : 'GET',
  50. update : 'PUT',
  51. destroy : 'DELETE'
  52. },
  53. * </code></pre>
  54. */
  55. restActions : {
  56. create : 'POST',
  57. read : 'GET',
  58. update : 'PUT',
  59. destroy : 'DELETE'
  60. },
  61. /**
  62. * Returns true if supplied action-name is a valid API action defined in <code>{@link #actions}</code> constants
  63. * @param {String} action
  64. * @param {String[]}(Optional) List of available CRUD actions. Pass in list when executing multiple times for efficiency.
  65. * @return {Boolean}
  66. */
  67. isAction : function(action) {
  68. return (Ext.data.Api.actions[action]) ? true : false;
  69. },
  70. /**
  71. * Returns the actual CRUD action KEY "create", "read", "update" or "destroy" from the supplied action-name. This method is used internally and shouldn't generally
  72. * need to be used directly. The key/value pair of Ext.data.Api.actions will often be identical but this is not necessarily true. A developer can override this naming
  73. * convention if desired. However, the framework internally calls methods based upon the KEY so a way of retreiving the the words "create", "read", "update" and "destroy" is
  74. * required. This method will cache discovered KEYS into the private validActions hash.
  75. * @param {String} name The runtime name of the action.
  76. * @return {String||null} returns the action-key, or verb of the user-action or null if invalid.
  77. * @nodoc
  78. */
  79. getVerb : function(name) {
  80. if (validActions[name]) {
  81. return validActions[name]; // <-- found in cache. return immediately.
  82. }
  83. for (var verb in this.actions) {
  84. if (this.actions[verb] === name) {
  85. validActions[name] = verb;
  86. break;
  87. }
  88. }
  89. return (validActions[name] !== undefined) ? validActions[name] : null;
  90. },
  91. /**
  92. * Returns true if the supplied API is valid; that is, check that all keys match defined actions
  93. * otherwise returns an array of mistakes.
  94. * @return {String[]||true}
  95. */
  96. isValid : function(api){
  97. var invalid = [];
  98. var crud = this.actions; // <-- cache a copy of the actions.
  99. for (var action in api) {
  100. if (!(action in crud)) {
  101. invalid.push(action);
  102. }
  103. }
  104. return (!invalid.length) ? true : invalid;
  105. },
  106. /**
  107. * Returns true if the supplied verb upon the supplied proxy points to a unique url in that none of the other api-actions
  108. * point to the same url. The question is important for deciding whether to insert the "xaction" HTTP parameter within an
  109. * Ajax request. This method is used internally and shouldn't generally need to be called directly.
  110. * @param {Ext.data.DataProxy} proxy
  111. * @param {String} verb
  112. * @return {Boolean}
  113. */
  114. hasUniqueUrl : function(proxy, verb) {
  115. var url = (proxy.api[verb]) ? proxy.api[verb].url : null;
  116. var unique = true;
  117. for (var action in proxy.api) {
  118. if ((unique = (action === verb) ? true : (proxy.api[action].url != url) ? true : false) === false) {
  119. break;
  120. }
  121. }
  122. return unique;
  123. },
  124. /**
  125. * This method is used internally by <tt>{@link Ext.data.DataProxy DataProxy}</tt> and should not generally need to be used directly.
  126. * Each action of a DataProxy api can be initially defined as either a String or an Object. When specified as an object,
  127. * one can explicitly define the HTTP method (GET|POST) to use for each CRUD action. This method will prepare the supplied API, setting
  128. * each action to the Object form. If your API-actions do not explicitly define the HTTP method, the "method" configuration-parameter will
  129. * be used. If the method configuration parameter is not specified, POST will be used.
  130. <pre><code>
  131. new Ext.data.HttpProxy({
  132. method: "POST", // <-- default HTTP method when not specified.
  133. api: {
  134. create: 'create.php',
  135. load: 'read.php',
  136. save: 'save.php',
  137. destroy: 'destroy.php'
  138. }
  139. });
  140. // Alternatively, one can use the object-form to specify the API
  141. new Ext.data.HttpProxy({
  142. api: {
  143. load: {url: 'read.php', method: 'GET'},
  144. create: 'create.php',
  145. destroy: 'destroy.php',
  146. save: 'update.php'
  147. }
  148. });
  149. </code></pre>
  150. *
  151. * @param {Ext.data.DataProxy} proxy
  152. */
  153. prepare : function(proxy) {
  154. if (!proxy.api) {
  155. proxy.api = {}; // <-- No api? create a blank one.
  156. }
  157. for (var verb in this.actions) {
  158. var action = this.actions[verb];
  159. proxy.api[action] = proxy.api[action] || proxy.url || proxy.directFn;
  160. if (typeof(proxy.api[action]) == 'string') {
  161. proxy.api[action] = {
  162. url: proxy.api[action]
  163. };
  164. }
  165. }
  166. },
  167. /**
  168. * Prepares a supplied Proxy to be RESTful. Sets the HTTP method for each api-action to be one of
  169. * GET, POST, PUT, DELETE according to the defined {@link #restActions}.
  170. * @param {Ext.data.DataProxy} proxy
  171. */
  172. restify : function(proxy) {
  173. proxy.restful = true;
  174. for (var verb in this.restActions) {
  175. proxy.api[this.actions[verb]].method = this.restActions[verb];
  176. }
  177. }
  178. };
  179. })();
  180. /**
  181. * @class Ext.data.Api.Error
  182. * @extends Ext.Error
  183. * Error class for Ext.data.Api errors
  184. */
  185. Ext.data.Api.Error = Ext.extend(Ext.Error, {
  186. constructor : function(message, arg) {
  187. this.arg = arg;
  188. Ext.Error.call(this, message);
  189. },
  190. name: 'Ext.data.Api'
  191. });
  192. Ext.apply(Ext.data.Api.Error.prototype, {
  193. lang: {
  194. 'action-url-undefined': 'No fallback url defined for this action. When defining a DataProxy api, please be sure to define an url for each CRUD action in Ext.data.Api.actions or define a default url in addition to your api-configuration.',
  195. 'invalid': 'received an invalid API-configuration. Please ensure your proxy API-configuration contains only the actions defined in Ext.data.Api.actions',
  196. 'invalid-url': 'Invalid url. Please review your proxy configuration.',
  197. 'execute': 'Attempted to execute an unknown action. Valid API actions are defined in Ext.data.Api.actions"'
  198. }
  199. });