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