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.

172 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-XmlWriter'>/**
  19. </span> * @class Ext.data.XmlWriter
  20. * @extends Ext.data.DataWriter
  21. * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action via XML.
  22. * XmlWriter uses an instance of {@link Ext.XTemplate} for maximum flexibility in defining your own custom XML schema if the default schema is not appropriate for your needs.
  23. * See the {@link #tpl} configuration-property.
  24. */
  25. Ext.data.XmlWriter = function(params) {
  26. Ext.data.XmlWriter.superclass.constructor.apply(this, arguments);
  27. // compile the XTemplate for rendering XML documents.
  28. this.tpl = (typeof(this.tpl) === 'string') ? new Ext.XTemplate(this.tpl).compile() : this.tpl.compile();
  29. };
  30. Ext.extend(Ext.data.XmlWriter, Ext.data.DataWriter, {
  31. <span id='Ext-data-XmlWriter-cfg-documentRoot'> /**
  32. </span> * @cfg {String} documentRoot [xrequest] (Optional) The name of the XML document root-node. &lt;b&gt;Note:&lt;/b&gt;
  33. * this parameter is required &lt;/b&gt;only when&lt;/b&gt; sending extra {@link Ext.data.Store#baseParams baseParams} to the server
  34. * during a write-request -- if no baseParams are set, the {@link Ext.data.XmlReader#record} meta-property can
  35. * suffice as the XML document root-node for write-actions involving just a &lt;b&gt;single record&lt;/b&gt;. For requests
  36. * involving &lt;b&gt;multiple&lt;/b&gt; records and &lt;b&gt;NO&lt;/b&gt; baseParams, the {@link Ext.data.XmlWriter#root} property can
  37. * act as the XML document root.
  38. */
  39. documentRoot: 'xrequest',
  40. <span id='Ext-data-XmlWriter-cfg-forceDocumentRoot'> /**
  41. </span> * @cfg {Boolean} forceDocumentRoot [false] Set to &lt;tt&gt;true&lt;/tt&gt; to force XML documents having a root-node as defined
  42. * by {@link #documentRoot}, even with no baseParams defined.
  43. */
  44. forceDocumentRoot: false,
  45. <span id='Ext-data-XmlWriter-cfg-root'> /**
  46. </span> * @cfg {String} root [records] The name of the containing element which will contain the nodes of an write-action involving &lt;b&gt;multiple&lt;/b&gt; records. Each
  47. * xml-record written to the server will be wrapped in an element named after {@link Ext.data.XmlReader#record} property.
  48. * eg:
  49. &lt;code&gt;&lt;pre&gt;
  50. &amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  51. &amp;lt;user&gt;&amp;lt;first&gt;Barney&amp;lt;/first&gt;&amp;lt;/user&gt;
  52. &lt;/code&gt;&lt;/pre&gt;
  53. * However, when &lt;b&gt;multiple&lt;/b&gt; records are written in a batch-operation, these records must be wrapped in a containing
  54. * Element.
  55. * eg:
  56. &lt;code&gt;&lt;pre&gt;
  57. &amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  58. &amp;lt;records&gt;
  59. &amp;lt;first&gt;Barney&amp;lt;/first&gt;&amp;lt;/user&gt;
  60. &amp;lt;records&gt;&amp;lt;first&gt;Barney&amp;lt;/first&gt;&amp;lt;/user&gt;
  61. &amp;lt;/records&gt;
  62. &lt;/code&gt;&lt;/pre&gt;
  63. * Defaults to &lt;tt&gt;records&lt;/tt&gt;. Do not confuse the nature of this property with that of {@link #documentRoot}
  64. */
  65. root: 'records',
  66. <span id='Ext-data-XmlWriter-cfg-xmlVersion'> /**
  67. </span> * @cfg {String} xmlVersion [1.0] The &lt;tt&gt;version&lt;/tt&gt; written to header of xml documents.
  68. &lt;code&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-15&quot;?&gt;&lt;/pre&gt;&lt;/code&gt;
  69. */
  70. xmlVersion : '1.0',
  71. <span id='Ext-data-XmlWriter-cfg-xmlEncoding'> /**
  72. </span> * @cfg {String} xmlEncoding [ISO-8859-15] The &lt;tt&gt;encoding&lt;/tt&gt; written to header of xml documents.
  73. &lt;code&gt;&lt;pre&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-15&quot;?&gt;&lt;/pre&gt;&lt;/code&gt;
  74. */
  75. xmlEncoding: 'ISO-8859-15',
  76. <span id='Ext-data-XmlWriter-cfg-tpl'> /**
  77. </span> * @cfg {String/Ext.XTemplate} tpl The XML template used to render {@link Ext.data.Api#actions write-actions} to your server.
  78. * &lt;p&gt;One can easily provide his/her own custom {@link Ext.XTemplate#constructor template-definition} if the default does not suffice.&lt;/p&gt;
  79. * &lt;p&gt;Defaults to:&lt;/p&gt;
  80. &lt;code&gt;&lt;pre&gt;
  81. &amp;lt;?xml version=&quot;{version}&quot; encoding=&quot;{encoding}&quot;?&gt;
  82. &amp;lt;tpl if=&quot;documentRoot&quot;&gt;&amp;lt;{documentRoot}&gt;
  83. &amp;lt;tpl for=&quot;baseParams&quot;&gt;
  84. &amp;lt;tpl for=&quot;.&quot;&gt;
  85. &amp;lt;{name}&gt;{value}&amp;lt;/{name}&gt;
  86. &amp;lt;/tpl&gt;
  87. &amp;lt;/tpl&gt;
  88. &amp;lt;tpl if=&quot;records.length &amp;gt; 1&quot;&gt;&amp;lt;{root}&gt;',
  89. &amp;lt;tpl for=&quot;records&quot;&gt;
  90. &amp;lt;{parent.record}&gt;
  91. &amp;lt;tpl for=&quot;.&quot;&gt;
  92. &amp;lt;{name}&gt;{value}&amp;lt;/{name}&gt;
  93. &amp;lt;/tpl&gt;
  94. &amp;lt;/{parent.record}&gt;
  95. &amp;lt;/tpl&gt;
  96. &amp;lt;tpl if=&quot;records.length &amp;gt; 1&quot;&gt;&amp;lt;/{root}&gt;&amp;lt;/tpl&gt;
  97. &amp;lt;tpl if=&quot;documentRoot&quot;&gt;&amp;lt;/{documentRoot}&gt;&amp;lt;/tpl&gt;
  98. &lt;/pre&gt;&lt;/code&gt;
  99. * &lt;p&gt;Templates will be called with the following API&lt;/p&gt;
  100. * &lt;ul&gt;
  101. * &lt;li&gt;{String} version [1.0] The xml version.&lt;/li&gt;
  102. * &lt;li&gt;{String} encoding [ISO-8859-15] The xml encoding.&lt;/li&gt;
  103. * &lt;li&gt;{String/false} documentRoot The XML document root-node name or &lt;tt&gt;false&lt;/tt&gt; if not required. See {@link #documentRoot} and {@link #forceDocumentRoot}.&lt;/li&gt;
  104. * &lt;li&gt;{String} record The meta-data parameter defined on your {@link Ext.data.XmlReader#record} configuration represents the name of the xml-tag containing each record.&lt;/li&gt;
  105. * &lt;li&gt;{String} root The meta-data parameter defined by {@link Ext.data.XmlWriter#root} configuration-parameter. Represents the name of the xml root-tag when sending &lt;b&gt;multiple&lt;/b&gt; records to the server.&lt;/li&gt;
  106. * &lt;li&gt;{Array} records The records being sent to the server, ie: the subject of the write-action being performed. The records parameter will be always be an array, even when only a single record is being acted upon.
  107. * Each item within the records array will contain an array of field objects having the following properties:
  108. * &lt;ul&gt;
  109. * &lt;li&gt;{String} name The field-name of the record as defined by your {@link Ext.data.Record#create Ext.data.Record definition}. The &quot;mapping&quot; property will be used, otherwise it will match the &quot;name&quot; property. Use this parameter to define the XML tag-name of the property.&lt;/li&gt;
  110. * &lt;li&gt;{Mixed} value The record value of the field enclosed within XML tags specified by name property above.&lt;/li&gt;
  111. * &lt;/ul&gt;&lt;/li&gt;
  112. * &lt;li&gt;{Array} baseParams. The baseParams as defined upon {@link Ext.data.Store#baseParams}. Note that the baseParams have been converted into an array of [{name : &quot;foo&quot;, value: &quot;bar&quot;}, ...] pairs in the same manner as the &lt;b&gt;records&lt;/b&gt; parameter above. See {@link #documentRoot} and {@link #forceDocumentRoot}.&lt;/li&gt;
  113. * &lt;/ul&gt;
  114. */
  115. // Encoding the ? here in case it's being included by some kind of page that will parse it (eg. PHP)
  116. tpl: '&lt;tpl for=&quot;.&quot;&gt;&lt;\u003fxml version=&quot;{version}&quot; encoding=&quot;{encoding}&quot;\u003f&gt;&lt;tpl if=&quot;documentRoot&quot;&gt;&lt;{documentRoot}&gt;&lt;tpl for=&quot;baseParams&quot;&gt;&lt;tpl for=&quot;.&quot;&gt;&lt;{name}&gt;{value}&lt;/{name}&gt;&lt;/tpl&gt;&lt;/tpl&gt;&lt;/tpl&gt;&lt;tpl if=&quot;records.length&amp;gt;1&quot;&gt;&lt;{root}&gt;&lt;/tpl&gt;&lt;tpl for=&quot;records&quot;&gt;&lt;{parent.record}&gt;&lt;tpl for=&quot;.&quot;&gt;&lt;{name}&gt;{value}&lt;/{name}&gt;&lt;/tpl&gt;&lt;/{parent.record}&gt;&lt;/tpl&gt;&lt;tpl if=&quot;records.length&amp;gt;1&quot;&gt;&lt;/{root}&gt;&lt;/tpl&gt;&lt;tpl if=&quot;documentRoot&quot;&gt;&lt;/{documentRoot}&gt;&lt;/tpl&gt;&lt;/tpl&gt;',
  117. <span id='Ext-data-XmlWriter-method-render'> /**
  118. </span> * XmlWriter implementation of the final stage of a write action.
  119. * @param {Object} params Transport-proxy's (eg: {@link Ext.Ajax#request}) params-object to write-to.
  120. * @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}.
  121. * @param {Object/Object[]} data Data-object representing the compiled Store-recordset.
  122. */
  123. render : function(params, baseParams, data) {
  124. baseParams = this.toArray(baseParams);
  125. params.xmlData = this.tpl.applyTemplate({
  126. version: this.xmlVersion,
  127. encoding: this.xmlEncoding,
  128. documentRoot: (baseParams.length &gt; 0 || this.forceDocumentRoot === true) ? this.documentRoot : false,
  129. record: this.meta.record,
  130. root: this.root,
  131. baseParams: baseParams,
  132. records: (Ext.isArray(data[0])) ? data : [data]
  133. });
  134. },
  135. <span id='Ext-data-XmlWriter-method-createRecord'> /**
  136. </span> * createRecord
  137. * @protected
  138. * @param {Ext.data.Record} rec
  139. * @return {Array} Array of &lt;tt&gt;name:value&lt;/tt&gt; pairs for attributes of the {@link Ext.data.Record}. See {@link Ext.data.DataWriter#toHash}.
  140. */
  141. createRecord : function(rec) {
  142. return this.toArray(this.toHash(rec));
  143. },
  144. <span id='Ext-data-XmlWriter-method-updateRecord'> /**
  145. </span> * updateRecord
  146. * @protected
  147. * @param {Ext.data.Record} rec
  148. * @return {Array} Array of {name:value} pairs for attributes of the {@link Ext.data.Record}. See {@link Ext.data.DataWriter#toHash}.
  149. */
  150. updateRecord : function(rec) {
  151. return this.toArray(this.toHash(rec));
  152. },
  153. <span id='Ext-data-XmlWriter-method-destroyRecord'> /**
  154. </span> * destroyRecord
  155. * @protected
  156. * @param {Ext.data.Record} rec
  157. * @return {Array} Array containing a attribute-object (name/value pair) representing the {@link Ext.data.DataReader#idProperty idProperty}.
  158. */
  159. destroyRecord : function(rec) {
  160. var data = {};
  161. data[this.meta.idProperty] = rec.id;
  162. return this.toArray(data);
  163. }
  164. });
  165. </pre>
  166. </body>
  167. </html>