3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.data.XmlWriter"></div>/**
10 * @class Ext.data.XmlWriter
11 * @extends Ext.data.DataWriter
12 * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action via XML.
13 * 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.
14 * See the {@link #tpl} configuration-property.
16 Ext.data.XmlWriter = function(params) {
17 Ext.data.XmlWriter.superclass.constructor.apply(this, arguments);
18 // compile the XTemplate for rendering XML documents.
19 this.tpl = (typeof(this.tpl) === 'string') ? new Ext.XTemplate(this.tpl).compile() : this.tpl.compile();
21 Ext.extend(Ext.data.XmlWriter, Ext.data.DataWriter, {
22 <div id="cfg-Ext.data.XmlWriter-documentRoot"></div>/**
23 * @cfg {String} documentRoot [xrequest] (Optional) The name of the XML document root-node. <b>Note:</b>
24 * this parameter is required </b>only when</b> sending extra {@link Ext.data.Store#baseParams baseParams} to the server
25 * during a write-request -- if no baseParams are set, the {@link Ext.data.XmlReader#record} meta-property can
26 * suffice as the XML document root-node for write-actions involving just a <b>single record</b>. For requests
27 * involving <b>multiple</b> records and <b>NO</b> baseParams, the {@link Ext.data.XmlWriter#root} property can
28 * act as the XML document root.
30 documentRoot: 'xrequest',
31 <div id="cfg-Ext.data.XmlWriter-forceDocumentRoot"></div>/**
32 * @cfg {Boolean} forceDocumentRoot [false] Set to <tt>true</tt> to force XML documents having a root-node as defined
33 * by {@link #documentRoot}, even with no baseParams defined.
35 forceDocumentRoot: false,
36 <div id="cfg-Ext.data.XmlWriter-root"></div>/**
37 * @cfg {String} root [records] The name of the containing element which will contain the nodes of an write-action involving <b>multiple</b> records. Each
38 * xml-record written to the server will be wrapped in an element named after {@link Ext.data.XmlReader#record} property.
41 <?xml version="1.0" encoding="UTF-8"?>
42 <user><first>Barney</first></user>
44 * However, when <b>multiple</b> records are written in a batch-operation, these records must be wrapped in a containing
48 <?xml version="1.0" encoding="UTF-8"?>
50 <first>Barney</first></user>
51 <records><first>Barney</first></user>
54 * Defaults to <tt>records</tt>. Do not confuse the nature of this property with that of {@link #documentRoot}
57 <div id="cfg-Ext.data.XmlWriter-xmlVersion"></div>/**
58 * @cfg {String} xmlVersion [1.0] The <tt>version</tt> written to header of xml documents.
59 <code><pre><?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
62 <div id="cfg-Ext.data.XmlWriter-xmlEncoding"></div>/**
63 * @cfg {String} xmlEncoding [ISO-8859-15] The <tt>encoding</tt> written to header of xml documents.
64 <code><pre><?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
66 xmlEncoding: 'ISO-8859-15',
67 <div id="cfg-Ext.data.XmlWriter-tpl"></div>/**
68 * @cfg {String/Ext.XTemplate} tpl The XML template used to render {@link Ext.data.Api#actions write-actions} to your server.
69 * <p>One can easily provide his/her own custom {@link Ext.XTemplate#constructor template-definition} if the default does not suffice.</p>
72 <?xml version="{version}" encoding="{encoding}"?>
73 <tpl if="documentRoot"><{documentRoot}>
74 <tpl for="baseParams">
76 <{name}>{value}</{name}>
79 <tpl if="records.length > 1"><{root}>',
80 <tpl for="records">
83 <{name}>{value}</{name}>
87 <tpl if="records.length > 1"></{root}></tpl>
88 <tpl if="documentRoot"></{documentRoot}></tpl>
90 * <p>Templates will be called with the following API</p>
92 * <li>{String} version [1.0] The xml version.</li>
93 * <li>{String} encoding [ISO-8859-15] The xml encoding.</li>
94 * <li>{String/false} documentRoot The XML document root-node name or <tt>false</tt> if not required. See {@link #documentRoot} and {@link #forceDocumentRoot}.</li>
95 * <li>{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.</li>
96 * <li>{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 <b>multiple</b> records to the server.</li>
97 * <li>{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.
98 * Each item within the records array will contain an array of field objects having the following properties:
100 * <li>{String} name The field-name of the record as defined by your {@link Ext.data.Record#create Ext.data.Record definition}. The "mapping" property will be used, otherwise it will match the "name" property. Use this parameter to define the XML tag-name of the property.</li>
101 * <li>{Mixed} value The record value of the field enclosed within XML tags specified by name property above.</li>
103 * <li>{Array} baseParams. The baseParams as defined upon {@link Ext.data.Store#baseParams}. Note that the baseParams have been converted into an array of [{name : "foo", value: "bar"}, ...] pairs in the same manner as the <b>records</b> parameter above. See {@link #documentRoot} and {@link #forceDocumentRoot}.</li>
106 // Break up encoding here in case it's being included by some kind of page that will parse it (eg. PHP)
107 tpl: '<tpl for="."><' + '?xml version="{version}" encoding="{encoding}"?' + '><tpl if="documentRoot"><{documentRoot}><tpl for="baseParams"><tpl for="."><{name}>{value}</{name}</tpl></tpl></tpl><tpl if="records.length>1"><{root}></tpl><tpl for="records"><{parent.record}><tpl for="."><{name}>{value}</{name}></tpl></{parent.record}></tpl><tpl if="records.length>1"></{root}></tpl><tpl if="documentRoot"></{documentRoot}></tpl></tpl>',
109 <div id="method-Ext.data.XmlWriter-render"></div>/**
110 * XmlWriter implementation of the final stage of a write action.
111 * @param {Object} params Transport-proxy's (eg: {@link Ext.Ajax#request}) params-object to write-to.
112 * @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}.
113 * @param {Object/Object[]} data Data-object representing the compiled Store-recordset.
115 render : function(params, baseParams, data) {
116 baseParams = this.toArray(baseParams);
117 params.xmlData = this.tpl.applyTemplate({
118 version: this.xmlVersion,
119 encoding: this.xmlEncoding,
120 documentRoot: (baseParams.length > 0 || this.forceDocumentRoot === true) ? this.documentRoot : false,
121 record: this.meta.record,
123 baseParams: baseParams,
124 records: (Ext.isArray(data[0])) ? data : [data]
128 <div id="method-Ext.data.XmlWriter-createRecord"></div>/**
131 * @param {Ext.data.Record} rec
132 * @return {Array} Array of <tt>name:value</tt> pairs for attributes of the {@link Ext.data.Record}. See {@link Ext.data.DataWriter#toHash}.
134 createRecord : function(rec) {
135 return this.toArray(this.toHash(rec));
138 <div id="method-Ext.data.XmlWriter-updateRecord"></div>/**
141 * @param {Ext.data.Record} rec
142 * @return {Array} Array of {name:value} pairs for attributes of the {@link Ext.data.Record}. See {@link Ext.data.DataWriter#toHash}.
144 updateRecord : function(rec) {
145 return this.toArray(this.toHash(rec));
148 <div id="method-Ext.data.XmlWriter-destroyRecord"></div>/**
151 * @param {Ext.data.Record} rec
152 * @return {Array} Array containing a attribute-object (name/value pair) representing the {@link Ext.data.DataReader#idProperty idProperty}.
154 destroyRecord : function(rec) {
156 data[this.meta.idProperty] = rec.id;
157 return this.toArray(data);