You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
11 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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-DataWriter-method-constructor'><span id='Ext-data-DataWriter'>/**
  19. </span></span> * @class Ext.data.DataWriter
  20. * &lt;p&gt;Ext.data.DataWriter facilitates create, update, and destroy actions between
  21. * an Ext.data.Store and a server-side framework. A Writer enabled Store will
  22. * automatically manage the Ajax requests to perform CRUD actions on a Store.&lt;/p&gt;
  23. * &lt;p&gt;Ext.data.DataWriter is an abstract base class which is intended to be extended
  24. * and should not be created directly. For existing implementations, see
  25. * {@link Ext.data.JsonWriter}.&lt;/p&gt;
  26. * &lt;p&gt;Creating a writer is simple:&lt;/p&gt;
  27. * &lt;pre&gt;&lt;code&gt;
  28. var writer = new Ext.data.JsonWriter({
  29. encode: false // &amp;lt;--- false causes data to be printed to jsonData config-property of Ext.Ajax#reqeust
  30. });
  31. * &lt;/code&gt;&lt;/pre&gt;
  32. * * &lt;p&gt;Same old JsonReader as Ext-2.x:&lt;/p&gt;
  33. * &lt;pre&gt;&lt;code&gt;
  34. var reader = new Ext.data.JsonReader({idProperty: 'id'}, [{name: 'first'}, {name: 'last'}, {name: 'email'}]);
  35. * &lt;/code&gt;&lt;/pre&gt;
  36. *
  37. * &lt;p&gt;The proxy for a writer enabled store can be configured with a simple &lt;code&gt;url&lt;/code&gt;:&lt;/p&gt;
  38. * &lt;pre&gt;&lt;code&gt;
  39. // Create a standard HttpProxy instance.
  40. var proxy = new Ext.data.HttpProxy({
  41. url: 'app.php/users' // &amp;lt;--- Supports &quot;provides&quot;-type urls, such as '/users.json', '/products.xml' (Hello Rails/Merb)
  42. });
  43. * &lt;/code&gt;&lt;/pre&gt;
  44. * &lt;p&gt;For finer grained control, the proxy may also be configured with an &lt;code&gt;API&lt;/code&gt;:&lt;/p&gt;
  45. * &lt;pre&gt;&lt;code&gt;
  46. // Maximum flexibility with the API-configuration
  47. var proxy = new Ext.data.HttpProxy({
  48. api: {
  49. read : 'app.php/users/read',
  50. create : 'app.php/users/create',
  51. update : 'app.php/users/update',
  52. destroy : { // &amp;lt;--- Supports object-syntax as well
  53. url: 'app.php/users/destroy',
  54. method: &quot;DELETE&quot;
  55. }
  56. }
  57. });
  58. * &lt;/code&gt;&lt;/pre&gt;
  59. * &lt;p&gt;Pulling it all together into a Writer-enabled Store:&lt;/p&gt;
  60. * &lt;pre&gt;&lt;code&gt;
  61. var store = new Ext.data.Store({
  62. proxy: proxy,
  63. reader: reader,
  64. writer: writer,
  65. autoLoad: true,
  66. autoSave: true // -- Cell-level updates.
  67. });
  68. * &lt;/code&gt;&lt;/pre&gt;
  69. * &lt;p&gt;Initiating write-actions &lt;b&gt;automatically&lt;/b&gt;, using the existing Ext2.0 Store/Record API:&lt;/p&gt;
  70. * &lt;pre&gt;&lt;code&gt;
  71. var rec = store.getAt(0);
  72. rec.set('email', 'foo@bar.com'); // &amp;lt;--- Immediately initiates an UPDATE action through configured proxy.
  73. store.remove(rec); // &amp;lt;---- Immediately initiates a DESTROY action through configured proxy.
  74. * &lt;/code&gt;&lt;/pre&gt;
  75. * &lt;p&gt;For &lt;b&gt;record/batch&lt;/b&gt; updates, use the Store-configuration {@link Ext.data.Store#autoSave autoSave:false}&lt;/p&gt;
  76. * &lt;pre&gt;&lt;code&gt;
  77. var store = new Ext.data.Store({
  78. proxy: proxy,
  79. reader: reader,
  80. writer: writer,
  81. autoLoad: true,
  82. autoSave: false // -- disable cell-updates
  83. });
  84. var urec = store.getAt(0);
  85. urec.set('email', 'foo@bar.com');
  86. var drec = store.getAt(1);
  87. store.remove(drec);
  88. // Push the button!
  89. store.save();
  90. * &lt;/code&gt;&lt;/pre&gt;
  91. * @constructor Create a new DataWriter
  92. * @param {Object} meta Metadata configuration options (implementation-specific)
  93. * @param {Object} recordType Either an Array of field definition objects as specified
  94. * in {@link Ext.data.Record#create}, or an {@link Ext.data.Record} object created
  95. * using {@link Ext.data.Record#create}.
  96. */
  97. Ext.data.DataWriter = function(config){
  98. Ext.apply(this, config);
  99. };
  100. Ext.data.DataWriter.prototype = {
  101. <span id='Ext-data-DataWriter-cfg-writeAllFields'> /**
  102. </span> * @cfg {Boolean} writeAllFields
  103. * &lt;tt&gt;false&lt;/tt&gt; by default. Set &lt;tt&gt;true&lt;/tt&gt; to have DataWriter return ALL fields of a modified
  104. * record -- not just those that changed.
  105. * &lt;tt&gt;false&lt;/tt&gt; to have DataWriter only request modified fields from a record.
  106. */
  107. writeAllFields : false,
  108. <span id='Ext-data-DataWriter-cfg-listful'> /**
  109. </span> * @cfg {Boolean} listful
  110. * &lt;tt&gt;false&lt;/tt&gt; by default. Set &lt;tt&gt;true&lt;/tt&gt; to have the DataWriter &lt;b&gt;always&lt;/b&gt; write HTTP params as a list,
  111. * even when acting upon a single record.
  112. */
  113. listful : false, // &lt;-- listful is actually not used internally here in DataWriter. @see Ext.data.Store#execute.
  114. <span id='Ext-data-DataWriter-method-apply'> /**
  115. </span> * Compiles a Store recordset into a data-format defined by an extension such as {@link Ext.data.JsonWriter} or {@link Ext.data.XmlWriter} in preparation for a {@link Ext.data.Api#actions server-write action}. The first two params are similar similar in nature to {@link Ext#apply},
  116. * Where the first parameter is the &lt;i&gt;receiver&lt;/i&gt; of paramaters and the second, baseParams, &lt;i&gt;the source&lt;/i&gt;.
  117. * @param {Object} params The request-params receiver.
  118. * @param {Object} baseParams as defined by {@link Ext.data.Store#baseParams}. The baseParms must be encoded by the extending class, eg: {@link Ext.data.JsonWriter}, {@link Ext.data.XmlWriter}.
  119. * @param {String} action [{@link Ext.data.Api#actions create|update|destroy}]
  120. * @param {Record/Record[]} rs The recordset to write, the subject(s) of the write action.
  121. */
  122. apply : function(params, baseParams, action, rs) {
  123. var data = [],
  124. renderer = action + 'Record';
  125. // TODO implement @cfg listful here
  126. if (Ext.isArray(rs)) {
  127. Ext.each(rs, function(rec){
  128. data.push(this[renderer](rec));
  129. }, this);
  130. }
  131. else if (rs instanceof Ext.data.Record) {
  132. data = this[renderer](rs);
  133. }
  134. this.render(params, baseParams, data);
  135. },
  136. <span id='Ext-data-DataWriter-method-render'> /**
  137. </span> * abstract method meant to be overridden by all DataWriter extensions. It's the extension's job to apply the &quot;data&quot; to the &quot;params&quot;.
  138. * The data-object provided to render is populated with data according to the meta-info defined in the user's DataReader config,
  139. * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
  140. * @param {Record[]} rs Store recordset
  141. * @param {Object} params Http params to be sent to server.
  142. * @param {Object} data object populated according to DataReader meta-data.
  143. */
  144. render : Ext.emptyFn,
  145. <span id='Ext-data-DataWriter-cfg-updateRecord'> /**
  146. </span> * @cfg {Function} updateRecord Abstract method that should be implemented in all subclasses
  147. * (e.g.: {@link Ext.data.JsonWriter#updateRecord JsonWriter.updateRecord}
  148. */
  149. updateRecord : Ext.emptyFn,
  150. <span id='Ext-data-DataWriter-cfg-createRecord'> /**
  151. </span> * @cfg {Function} createRecord Abstract method that should be implemented in all subclasses
  152. * (e.g.: {@link Ext.data.JsonWriter#createRecord JsonWriter.createRecord})
  153. */
  154. createRecord : Ext.emptyFn,
  155. <span id='Ext-data-DataWriter-cfg-destroyRecord'> /**
  156. </span> * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses
  157. * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord})
  158. */
  159. destroyRecord : Ext.emptyFn,
  160. <span id='Ext-data-DataWriter-method-toHash'> /**
  161. </span> * Converts a Record to a hash, taking into account the state of the Ext.data.Record along with configuration properties
  162. * related to its rendering, such as {@link #writeAllFields}, {@link Ext.data.Record#phantom phantom}, {@link Ext.data.Record#getChanges getChanges} and
  163. * {@link Ext.data.DataReader#idProperty idProperty}
  164. * @param {Ext.data.Record} rec The Record from which to create a hash.
  165. * @param {Object} config &lt;b&gt;NOT YET IMPLEMENTED&lt;/b&gt;. Will implement an exlude/only configuration for fine-control over which fields do/don't get rendered.
  166. * @return {Object}
  167. * @protected
  168. * TODO Implement excludes/only configuration with 2nd param?
  169. */
  170. toHash : function(rec, config) {
  171. var map = rec.fields.map,
  172. data = {},
  173. raw = (this.writeAllFields === false &amp;&amp; rec.phantom === false) ? rec.getChanges() : rec.data,
  174. m;
  175. Ext.iterate(raw, function(prop, value){
  176. if((m = map[prop])){
  177. data[m.mapping ? m.mapping : m.name] = value;
  178. }
  179. });
  180. // we don't want to write Ext auto-generated id to hash. Careful not to remove it on Models not having auto-increment pk though.
  181. // We can tell its not auto-increment if the user defined a DataReader field for it *and* that field's value is non-empty.
  182. // we could also do a RegExp here for the Ext.data.Record AUTO_ID prefix.
  183. if (rec.phantom) {
  184. if (rec.fields.containsKey(this.meta.idProperty) &amp;&amp; Ext.isEmpty(rec.data[this.meta.idProperty])) {
  185. delete data[this.meta.idProperty];
  186. }
  187. } else {
  188. data[this.meta.idProperty] = rec.id;
  189. }
  190. return data;
  191. },
  192. <span id='Ext-data-DataWriter-method-toArray'> /**
  193. </span> * Converts a {@link Ext.data.DataWriter#toHash Hashed} {@link Ext.data.Record} to fields-array array suitable
  194. * for encoding to xml via XTemplate, eg:
  195. &lt;code&gt;&lt;pre&gt;&amp;lt;tpl for=&quot;.&quot;&gt;&amp;lt;{name}&gt;{value}&amp;lt;/{name}&amp;lt;/tpl&gt;&lt;/pre&gt;&lt;/code&gt;
  196. * eg, &lt;b&gt;non-phantom&lt;/b&gt;:
  197. &lt;code&gt;&lt;pre&gt;{id: 1, first: 'foo', last: 'bar'} --&gt; [{name: 'id', value: 1}, {name: 'first', value: 'foo'}, {name: 'last', value: 'bar'}]&lt;/pre&gt;&lt;/code&gt;
  198. * {@link Ext.data.Record#phantom Phantom} records will have had their idProperty omitted in {@link #toHash} if determined to be auto-generated.
  199. * Non AUTOINCREMENT pks should have been protected.
  200. * @param {Hash} data Hashed by Ext.data.DataWriter#toHash
  201. * @return {Object[]} Array of attribute-objects.
  202. * @protected
  203. */
  204. toArray : function(data) {
  205. var fields = [];
  206. Ext.iterate(data, function(k, v) {fields.push({name: k, value: v});},this);
  207. return fields;
  208. }
  209. };</pre>
  210. </body>
  211. </html>