Writer.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-writer-Writer'>/**
  19. </span> * @author Ed Spencer
  20. *
  21. * Base Writer class used by most subclasses of {@link Ext.data.proxy.Server}. This class is responsible for taking a
  22. * set of {@link Ext.data.Operation} objects and a {@link Ext.data.Request} object and modifying that request based on
  23. * the Operations.
  24. *
  25. * For example a Ext.data.writer.Json would format the Operations and their {@link Ext.data.Model} instances based on
  26. * the config options passed to the JsonWriter's constructor.
  27. *
  28. * Writers are not needed for any kind of local storage - whether via a {@link Ext.data.proxy.WebStorage Web Storage
  29. * proxy} (see {@link Ext.data.proxy.LocalStorage localStorage} and {@link Ext.data.proxy.SessionStorage
  30. * sessionStorage}) or just in memory via a {@link Ext.data.proxy.Memory MemoryProxy}.
  31. */
  32. Ext.define('Ext.data.writer.Writer', {
  33. alias: 'writer.base',
  34. alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],
  35. <span id='Ext-data-writer-Writer-cfg-writeAllFields'> /**
  36. </span> * @cfg {Boolean} writeAllFields
  37. * True to write all fields from the record to the server. If set to false it will only send the fields that were
  38. * modified. Note that any fields that have {@link Ext.data.Field#persist} set to false will still be ignored.
  39. */
  40. writeAllFields: true,
  41. <span id='Ext-data-writer-Writer-cfg-nameProperty'> /**
  42. </span> * @cfg {String} nameProperty
  43. * This property is used to read the key for each value that will be sent to the server. For example:
  44. *
  45. * Ext.define('Person', {
  46. * extend: 'Ext.data.Model',
  47. * fields: [{
  48. * name: 'first',
  49. * mapping: 'firstName'
  50. * }, {
  51. * name: 'last',
  52. * mapping: 'lastName'
  53. * }, {
  54. * name: 'age'
  55. * }]
  56. * });
  57. * new Ext.data.writer.Writer({
  58. * writeAllFields: true,
  59. * nameProperty: 'mapping'
  60. * });
  61. *
  62. * // This will be sent to the server
  63. * {
  64. * firstName: 'first name value',
  65. * lastName: 'last name value',
  66. * age: 1
  67. * }
  68. *
  69. * If the value is not present, the field name will always be used.
  70. */
  71. nameProperty: 'name',
  72. /*
  73. * @property {Boolean} isWriter
  74. * `true` in this class to identify an object as an instantiated Writer, or subclass thereof.
  75. */
  76. isWriter: true,
  77. <span id='Ext-data-writer-Writer-method-constructor'> /**
  78. </span> * Creates new Writer.
  79. * @param {Object} [config] Config object.
  80. */
  81. constructor: function(config) {
  82. Ext.apply(this, config);
  83. },
  84. <span id='Ext-data-writer-Writer-method-write'> /**
  85. </span> * Prepares a Proxy's Ext.data.Request object
  86. * @param {Ext.data.Request} request The request object
  87. * @return {Ext.data.Request} The modified request object
  88. */
  89. write: function(request) {
  90. var operation = request.operation,
  91. records = operation.records || [],
  92. len = records.length,
  93. i = 0,
  94. data = [];
  95. for (; i &lt; len; i++) {
  96. data.push(this.getRecordData(records[i], operation));
  97. }
  98. return this.writeRecords(request, data);
  99. },
  100. <span id='Ext-data-writer-Writer-method-getRecordData'> /**
  101. </span> * Formats the data for each record before sending it to the server. This
  102. * method should be overridden to format the data in a way that differs from the default.
  103. * @param {Ext.data.Model} record The record that we are writing to the server.
  104. * @param {Ext.data.Operation} [operation] An operation object.
  105. * @return {Object} An object literal of name/value keys to be written to the server.
  106. * By default this method returns the data property on the record.
  107. */
  108. getRecordData: function(record, operation) {
  109. var isPhantom = record.phantom === true,
  110. writeAll = this.writeAllFields || isPhantom,
  111. nameProperty = this.nameProperty,
  112. fields = record.fields,
  113. fieldItems = fields.items,
  114. data = {},
  115. clientIdProperty = record.clientIdProperty,
  116. changes,
  117. name,
  118. field,
  119. key,
  120. value,
  121. f, fLen;
  122. if (writeAll) {
  123. fLen = fieldItems.length;
  124. for (f = 0; f &lt; fLen; f++) {
  125. field = fieldItems[f];
  126. if (field.persist) {
  127. name = field[nameProperty] || field.name;
  128. value = record.get(field.name);
  129. if (field.serialize) {
  130. data[name] = field.serialize(value, record);
  131. } else if (field.type === Ext.data.Types.DATE &amp;&amp; field.dateFormat) {
  132. data[name] = Ext.Date.format(value, field.dateFormat);
  133. } else {
  134. data[name] = value;
  135. }
  136. }
  137. }
  138. } else {
  139. // Only write the changes
  140. changes = record.getChanges();
  141. for (key in changes) {
  142. if (changes.hasOwnProperty(key)) {
  143. field = fields.get(key);
  144. if (field.persist) {
  145. name = field[nameProperty] || field.name;
  146. value = record.get(field.name);
  147. if (field.serialize) {
  148. data[name] = field.serialize(value, record);
  149. } else if (field.type === Ext.data.Types.DATE &amp;&amp; field.dateFormat) {
  150. data[name] = Ext.Date.format(value, field.dateFormat);
  151. } else {
  152. data[name] = value;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. if (isPhantom) {
  159. if (clientIdProperty &amp;&amp; operation &amp;&amp; operation.records.length &gt; 1) {
  160. // include clientId for phantom records, if multiple records are being written to the server in one operation.
  161. // The server can then return the clientId with each record so the operation can match the server records with the client records
  162. data[clientIdProperty] = record.internalId;
  163. }
  164. } else {
  165. // always include the id for non phantoms
  166. data[record.idProperty] = record.getId();
  167. }
  168. return data;
  169. }
  170. });
  171. </pre>
  172. </body>
  173. </html>