/ext-4.0.7/docs/source/Writer.html

https://bitbucket.org/srogerf/javascript · HTML · 146 lines · 139 code · 7 blank · 0 comment · 0 complexity · 6c9ecf64913e8d1d58dfc2965f823506 MD5 · raw file

  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. <span id='Ext-data-writer-Writer-method-constructor'> /**
  73. </span> * Creates new Writer.
  74. * @param {Object} [config] Config object.
  75. */
  76. constructor: function(config) {
  77. Ext.apply(this, config);
  78. },
  79. <span id='Ext-data-writer-Writer-method-write'> /**
  80. </span> * Prepares a Proxy's Ext.data.Request object
  81. * @param {Ext.data.Request} request The request object
  82. * @return {Ext.data.Request} The modified request object
  83. */
  84. write: function(request) {
  85. var operation = request.operation,
  86. records = operation.records || [],
  87. len = records.length,
  88. i = 0,
  89. data = [];
  90. for (; i &lt; len; i++) {
  91. data.push(this.getRecordData(records[i]));
  92. }
  93. return this.writeRecords(request, data);
  94. },
  95. <span id='Ext-data-writer-Writer-method-getRecordData'> /**
  96. </span> * Formats the data for each record before sending it to the server. This method should be overridden to format the
  97. * data in a way that differs from the default.
  98. * @param {Object} record The record that we are writing to the server.
  99. * @return {Object} An object literal of name/value keys to be written to the server. By default this method returns
  100. * the data property on the record.
  101. */
  102. getRecordData: function(record) {
  103. var isPhantom = record.phantom === true,
  104. writeAll = this.writeAllFields || isPhantom,
  105. nameProperty = this.nameProperty,
  106. fields = record.fields,
  107. data = {},
  108. changes,
  109. name,
  110. field,
  111. key;
  112. if (writeAll) {
  113. fields.each(function(field){
  114. if (field.persist) {
  115. name = field[nameProperty] || field.name;
  116. data[name] = record.get(field.name);
  117. }
  118. });
  119. } else {
  120. // Only write the changes
  121. changes = record.getChanges();
  122. for (key in changes) {
  123. if (changes.hasOwnProperty(key)) {
  124. field = fields.get(key);
  125. name = field[nameProperty] || field.name;
  126. data[name] = changes[key];
  127. }
  128. }
  129. if (!isPhantom) {
  130. // always include the id for non phantoms
  131. data[record.idProperty] = record.getId();
  132. }
  133. }
  134. return data;
  135. }
  136. });
  137. </pre>
  138. </body>
  139. </html>