Memory.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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-Memory'>/**
  19. </span> * @author Ed Spencer
  20. *
  21. * In-memory proxy. This proxy simply uses a local variable for data storage/retrieval, so its contents are lost on
  22. * every page refresh.
  23. *
  24. * Usually this Proxy isn't used directly, serving instead as a helper to a {@link Ext.data.Store Store} where a reader
  25. * is required to load data. For example, say we have a Store for a User model and have some inline data we want to
  26. * load, but this data isn't in quite the right format: we can use a MemoryProxy with a JsonReader to read it into our
  27. * Store:
  28. *
  29. * //this is the model we will be using in the store
  30. * Ext.define('User', {
  31. * extend: 'Ext.data.Model',
  32. * fields: [
  33. * {name: 'id', type: 'int'},
  34. * {name: 'name', type: 'string'},
  35. * {name: 'phone', type: 'string', mapping: 'phoneNumber'}
  36. * ]
  37. * });
  38. *
  39. * //this data does not line up to our model fields - the phone field is called phoneNumber
  40. * var data = {
  41. * users: [
  42. * {
  43. * id: 1,
  44. * name: 'Ed Spencer',
  45. * phoneNumber: '555 1234'
  46. * },
  47. * {
  48. * id: 2,
  49. * name: 'Abe Elias',
  50. * phoneNumber: '666 1234'
  51. * }
  52. * ]
  53. * };
  54. *
  55. * //note how we set the 'root' in the reader to match the data structure above
  56. * var store = Ext.create('Ext.data.Store', {
  57. * autoLoad: true,
  58. * model: 'User',
  59. * data : data,
  60. * proxy: {
  61. * type: 'memory',
  62. * reader: {
  63. * type: 'json',
  64. * root: 'users'
  65. * }
  66. * }
  67. * });
  68. */
  69. Ext.define('Ext.data.proxy.Memory', {
  70. extend: 'Ext.data.proxy.Client',
  71. alias: 'proxy.memory',
  72. alternateClassName: 'Ext.data.MemoryProxy',
  73. <span id='Ext-data-proxy-Memory-cfg-data'> /**
  74. </span> * @cfg {Object} data
  75. * Optional data to pass to configured Reader.
  76. */
  77. constructor: function(config) {
  78. this.callParent([config]);
  79. //ensures that the reader has been instantiated properly
  80. this.setReader(this.reader);
  81. },
  82. <span id='Ext-data-proxy-Memory-method-updateOperation'> /**
  83. </span> * @private
  84. * Fake processing function to commit the records, set the current operation
  85. * to successful and call the callback if provided. This function is shared
  86. * by the create, update and destroy methods to perform the bare minimum
  87. * processing required for the proxy to register a result from the action.
  88. */
  89. updateOperation: function(operation, callback, scope) {
  90. var i = 0,
  91. recs = operation.getRecords(),
  92. len = recs.length;
  93. for (i; i &lt; len; i++) {
  94. recs[i].commit();
  95. }
  96. operation.setCompleted();
  97. operation.setSuccessful();
  98. Ext.callback(callback, scope || this, [operation]);
  99. },
  100. <span id='Ext-data-proxy-Memory-method-create'> /**
  101. </span> * Currently this is a hard-coded method that simply commits any records and sets the operation to successful,
  102. * then calls the callback function, if provided. It is essentially mocking a server call in memory, but since
  103. * there is no real back end in this case there's not much else to do. This method can be easily overridden to
  104. * implement more complex logic if needed.
  105. * @param {Ext.data.Operation} operation The Operation to perform
  106. * @param {Function} callback Callback function to be called when the Operation has completed (whether
  107. * successful or not)
  108. * @param {Object} scope Scope to execute the callback function in
  109. * @method
  110. */
  111. create: function() {
  112. this.updateOperation.apply(this, arguments);
  113. },
  114. <span id='Ext-data-proxy-Memory-method-update'> /**
  115. </span> * Currently this is a hard-coded method that simply commits any records and sets the operation to successful,
  116. * then calls the callback function, if provided. It is essentially mocking a server call in memory, but since
  117. * there is no real back end in this case there's not much else to do. This method can be easily overridden to
  118. * implement more complex logic if needed.
  119. * @param {Ext.data.Operation} operation The Operation to perform
  120. * @param {Function} callback Callback function to be called when the Operation has completed (whether
  121. * successful or not)
  122. * @param {Object} scope Scope to execute the callback function in
  123. * @method
  124. */
  125. update: function() {
  126. this.updateOperation.apply(this, arguments);
  127. },
  128. <span id='Ext-data-proxy-Memory-method-destroy'> /**
  129. </span> * Currently this is a hard-coded method that simply commits any records and sets the operation to successful,
  130. * then calls the callback function, if provided. It is essentially mocking a server call in memory, but since
  131. * there is no real back end in this case there's not much else to do. This method can be easily overridden to
  132. * implement more complex logic if needed.
  133. * @param {Ext.data.Operation} operation The Operation to perform
  134. * @param {Function} callback Callback function to be called when the Operation has completed (whether
  135. * successful or not)
  136. * @param {Object} scope Scope to execute the callback function in
  137. * @method
  138. */
  139. destroy: function() {
  140. this.updateOperation.apply(this, arguments);
  141. },
  142. <span id='Ext-data-proxy-Memory-method-read'> /**
  143. </span> * Reads data from the configured {@link #data} object. Uses the Proxy's {@link #reader}, if present.
  144. * @param {Ext.data.Operation} operation The read Operation
  145. * @param {Function} callback The callback to call when reading has completed
  146. * @param {Object} scope The scope to call the callback function in
  147. */
  148. read: function(operation, callback, scope) {
  149. var me = this;
  150. operation.resultSet = me.getReader().read(me.data);
  151. operation.setCompleted();
  152. operation.setSuccessful();
  153. Ext.callback(callback, scope || me, [operation]);
  154. },
  155. clear: Ext.emptyFn
  156. });
  157. </pre>
  158. </body>
  159. </html>