Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / data / XmlWriter.js
1 /*!
2  * Ext JS Library 3.0.3
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  */
12 Ext.data.XmlWriter = function(params) {
13     Ext.data.XmlWriter.superclass.constructor.apply(this, arguments);
14     this.tpl = new Ext.XTemplate(this.tpl).compile();
15 };
16 Ext.extend(Ext.data.XmlWriter, Ext.data.DataWriter, {
17     /**
18      * @cfg {String} root [records] The name of the root element when writing <b>multiple</b> records to the server.  Each
19      * xml-record written to the server will be wrapped in an element named after {@link Ext.data.XmlReader#record} property.
20      * eg:
21 <code><pre>
22 &lt;?xml version="1.0" encoding="UTF-8"?>
23 &lt;user>&lt;first>Barney&lt;/first>&lt;/user>
24 </code></pre>
25      * However, when <b>multiple</b> records are written in a batch-operation, these records must be wrapped in a containing
26      * Element.
27      * eg:
28 <code><pre>
29 &lt;?xml version="1.0" encoding="UTF-8"?>
30     &lt;records>
31         &lt;first>Barney&lt;/first>&lt;/user>
32         &lt;records>&lt;first>Barney&lt;/first>&lt;/user>
33     &lt;/records>
34 </code></pre>
35      * Defaults to <tt>records</tt>
36      */
37     root: 'records',
38     /**
39      * @cfg {String} xmlVersion [1.0] The <tt>version</tt> written to header of xml documents.
40 <code><pre>&lt;?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
41      */
42     xmlVersion : '1.0',
43     /**
44      * @cfg {String} xmlEncoding [ISO-8859-15] The <tt>encoding</tt> written to header of xml documents.
45 <code><pre>&lt;?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
46      */
47     xmlEncoding: 'ISO-8859-15',
48     /**
49      * @cfg {String} tpl The xml template.  Defaults to
50 <code><pre>
51 &lt;?xml version="{version}" encoding="{encoding}"?>
52     &lt;tpl if="{[values.nodes.length>1]}">&lt;{root}}>',
53     &lt;tpl for="records">
54         &lt;{parent.record}>
55         &lt;tpl for="fields">
56             &lt;{name}>{value}&lt;/{name}>
57         &lt;/tpl>
58         &lt;/{parent.record}>
59     &lt;/tpl>
60     &lt;tpl if="{[values.records.length>1]}">&lt;/{root}}>&lt;/tpl>
61 </pre></code>
62      */
63     // Break up encoding here in case it's being included by some kind of page that will parse it (eg. PHP)
64     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>',
65
66     /**
67      * Final action of a write event.  Apply the written data-object to params.
68      * @param {String} action [Ext.data.Api.create|read|update|destroy]
69      * @param {Ext.data.Record/Ext.data.Record[]} rs
70      * @param {Object} http params
71      * @param {Object/Object[]} rendered data.
72      */
73     render : function(action, rs, params, data) {
74         params.xmlData = this.tpl.applyTemplate({
75             version: this.xmlVersion,
76             encoding: this.xmlEncoding,
77             record: this.meta.record,
78             root: this.root,
79             records: (Ext.isArray(rs)) ? data : [data]
80         });
81     },
82
83     /**
84      * Converts an Ext.data.Record to xml
85      * @param {Ext.data.Record} rec
86      * @return {String} rendered xml-element
87      * @private
88      */
89     toXml : function(data) {
90         var fields = [];
91         Ext.iterate(data, function(k, v) {
92             fields.push({
93                 name: k,
94                 value: v
95             });
96         },this);
97         return {
98             fields: fields
99         };
100     },
101
102     /**
103      * createRecord
104      * @param {Ext.data.Record} rec
105      * @return {String} xml element
106      * @private
107      */
108     createRecord : function(rec) {
109         return this.toXml(this.toHash(rec));
110     },
111
112     /**
113      * updateRecord
114      * @param {Ext.data.Record} rec
115      * @return {String} xml element
116      * @private
117      */
118     updateRecord : function(rec) {
119         return this.toXml(this.toHash(rec));
120
121     },
122     /**
123      * destroyRecord
124      * @param {Ext.data.Record} rec
125      * @return {String} xml element
126      */
127     destroyRecord : function(rec) {
128         var data = {};
129         data[this.meta.idProperty] = rec.id;
130         return this.toXml(data);
131     }
132 });
133