3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
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.
12 Ext.data.XmlWriter = function(params) {
13 Ext.data.XmlWriter.superclass.constructor.apply(this, arguments);
14 this.tpl = new Ext.XTemplate(this.tpl).compile();
16 Ext.extend(Ext.data.XmlWriter, Ext.data.DataWriter, {
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.
22 <?xml version="1.0" encoding="UTF-8"?>
23 <user><first>Barney</first></user>
25 * However, when <b>multiple</b> records are written in a batch-operation, these records must be wrapped in a containing
29 <?xml version="1.0" encoding="UTF-8"?>
31 <first>Barney</first></user>
32 <records><first>Barney</first></user>
35 * Defaults to <tt>records</tt>
39 * @cfg {String} xmlVersion [1.0] The <tt>version</tt> written to header of xml documents.
40 <code><pre><?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
44 * @cfg {String} xmlEncoding [ISO-8859-15] The <tt>encoding</tt> written to header of xml documents.
45 <code><pre><?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
47 xmlEncoding: 'ISO-8859-15',
49 * @cfg {String} tpl The xml template. Defaults to
51 <?xml version="{version}" encoding="{encoding}"?>
52 <tpl if="{[values.nodes.length>1]}"><{root}}>',
53 <tpl for="records">
56 <{name}>{value}</{name}>
60 <tpl if="{[values.records.length>1]}"></{root}}></tpl>
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>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>',
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.
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,
79 records: (Ext.isArray(rs)) ? data : [data]
84 * Converts an Ext.data.Record to xml
85 * @param {Ext.data.Record} rec
86 * @return {String} rendered xml-element
89 toXml : function(data) {
91 Ext.iterate(data, function(k, v) {
104 * @param {Ext.data.Record} rec
105 * @return {String} xml element
108 createRecord : function(rec) {
109 return this.toXml(this.toHash(rec));
114 * @param {Ext.data.Record} rec
115 * @return {String} xml element
118 updateRecord : function(rec) {
119 return this.toXml(this.toHash(rec));
124 * @param {Ext.data.Record} rec
125 * @return {String} xml element
127 destroyRecord : function(rec) {
129 data[this.meta.idProperty] = rec.id;
130 return this.toXml(data);