PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/ExtJS/docs/source/DataWriter.html

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