Rest.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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-Rest'>/**
  19. </span> * @author Ed Spencer
  20. *
  21. * The Rest proxy is a specialization of the {@link Ext.data.proxy.Ajax AjaxProxy} which simply maps the four actions
  22. * (create, read, update and destroy) to RESTful HTTP verbs. For example, let's set up a {@link Ext.data.Model Model}
  23. * with an inline Rest proxy
  24. *
  25. * Ext.define('User', {
  26. * extend: 'Ext.data.Model',
  27. * fields: ['id', 'name', 'email'],
  28. *
  29. * proxy: {
  30. * type: 'rest',
  31. * url : '/users'
  32. * }
  33. * });
  34. *
  35. * Now we can create a new User instance and save it via the Rest proxy. Doing this will cause the Proxy to send a POST
  36. * request to '/users':
  37. *
  38. * var user = Ext.create('User', {name: 'Ed Spencer', email: 'ed@sencha.com'});
  39. *
  40. * user.save(); //POST /users
  41. *
  42. * Let's expand this a little and provide a callback for the {@link Ext.data.Model#save} call to update the Model once
  43. * it has been created. We'll assume the creation went successfully and that the server gave this user an ID of 123:
  44. *
  45. * user.save({
  46. * success: function(user) {
  47. * user.set('name', 'Khan Noonien Singh');
  48. *
  49. * user.save(); //PUT /users/123
  50. * }
  51. * });
  52. *
  53. * Now that we're no longer creating a new Model instance, the request method is changed to an HTTP PUT, targeting the
  54. * relevant url for that user. Now let's delete this user, which will use the DELETE method:
  55. *
  56. * user.destroy(); //DELETE /users/123
  57. *
  58. * Finally, when we perform a load of a Model or Store, Rest proxy will use the GET method:
  59. *
  60. * //1. Load via Store
  61. *
  62. * //the Store automatically picks up the Proxy from the User model
  63. * var store = Ext.create('Ext.data.Store', {
  64. * model: 'User'
  65. * });
  66. *
  67. * store.load(); //GET /users
  68. *
  69. * //2. Load directly from the Model
  70. *
  71. * //GET /users/123
  72. * Ext.ModelManager.getModel('User').load(123, {
  73. * success: function(user) {
  74. * console.log(user.getId()); //outputs 123
  75. * }
  76. * });
  77. *
  78. * # Url generation
  79. *
  80. * The Rest proxy is able to automatically generate the urls above based on two configuration options - {@link #appendId} and
  81. * {@link #format}. If appendId is true (it is by default) then Rest proxy will automatically append the ID of the Model
  82. * instance in question to the configured url, resulting in the '/users/123' that we saw above.
  83. *
  84. * If the request is not for a specific Model instance (e.g. loading a Store), the url is not appended with an id.
  85. * The Rest proxy will automatically insert a '/' before the ID if one is not already present.
  86. *
  87. * new Ext.data.proxy.Rest({
  88. * url: '/users',
  89. * appendId: true //default
  90. * });
  91. *
  92. * // Collection url: /users
  93. * // Instance url : /users/123
  94. *
  95. * The Rest proxy can also optionally append a format string to the end of any generated url:
  96. *
  97. * new Ext.data.proxy.Rest({
  98. * url: '/users',
  99. * format: 'json'
  100. * });
  101. *
  102. * // Collection url: /users.json
  103. * // Instance url : /users/123.json
  104. *
  105. * If further customization is needed, simply implement the {@link #buildUrl} method and add your custom generated url
  106. * onto the {@link Ext.data.Request Request} object that is passed to buildUrl. See [Rest proxy's implementation][1] for
  107. * an example of how to achieve this.
  108. *
  109. * Note that Rest proxy inherits from {@link Ext.data.proxy.Ajax AjaxProxy}, which already injects all of the sorter,
  110. * filter, group and paging options into the generated url. See the {@link Ext.data.proxy.Ajax AjaxProxy docs} for more
  111. * details.
  112. *
  113. * [1]: source/Rest.html#Ext-data-proxy-Rest-method-buildUrl
  114. */
  115. Ext.define('Ext.data.proxy.Rest', {
  116. extend: 'Ext.data.proxy.Ajax',
  117. alternateClassName: 'Ext.data.RestProxy',
  118. alias : 'proxy.rest',
  119. <span id='Ext-data-proxy-Rest-cfg-appendId'> /**
  120. </span> * @cfg {Boolean} appendId
  121. * True to automatically append the ID of a Model instance when performing a request based on that single instance.
  122. * See Rest proxy intro docs for more details. Defaults to true.
  123. */
  124. appendId: true,
  125. <span id='Ext-data-proxy-Rest-cfg-format'> /**
  126. </span> * @cfg {String} format
  127. * Optional data format to send to the server when making any request (e.g. 'json'). See the Rest proxy intro docs
  128. * for full details. Defaults to undefined.
  129. */
  130. <span id='Ext-data-proxy-Rest-cfg-batchActions'> /**
  131. </span> * @cfg {Boolean} batchActions
  132. * True to batch actions of a particular type when synchronizing the store. Defaults to false.
  133. */
  134. batchActions: false,
  135. <span id='Ext-data-proxy-Rest-method-buildUrl'> /**
  136. </span> * Specialized version of buildUrl that incorporates the {@link #appendId} and {@link #format} options into the
  137. * generated url. Override this to provide further customizations, but remember to call the superclass buildUrl so
  138. * that additional parameters like the cache buster string are appended.
  139. * @param {Object} request
  140. */
  141. buildUrl: function(request) {
  142. var me = this,
  143. operation = request.operation,
  144. records = operation.records || [],
  145. record = records[0],
  146. format = me.format,
  147. url = me.getUrl(request),
  148. id = record ? record.getId() : operation.id;
  149. if (me.appendId &amp;&amp; id) {
  150. if (!url.match(/\/$/)) {
  151. url += '/';
  152. }
  153. url += id;
  154. }
  155. if (format) {
  156. if (!url.match(/\.$/)) {
  157. url += '.';
  158. }
  159. url += format;
  160. }
  161. request.url = url;
  162. return me.callParent(arguments);
  163. }
  164. }, function() {
  165. Ext.apply(this.prototype, {
  166. <span id='Ext-data-proxy-Rest-property-actionMethods'> /**
  167. </span> * @property {Object} actionMethods
  168. * Mapping of action name to HTTP request method. These default to RESTful conventions for the 'create', 'read',
  169. * 'update' and 'destroy' actions (which map to 'POST', 'GET', 'PUT' and 'DELETE' respectively). This object
  170. * should not be changed except globally via {@link Ext#override Ext.override} - the {@link #getMethod} function
  171. * can be overridden instead.
  172. */
  173. actionMethods: {
  174. create : 'POST',
  175. read : 'GET',
  176. update : 'PUT',
  177. destroy: 'DELETE'
  178. }
  179. });
  180. });
  181. </pre>
  182. </body>
  183. </html>