3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.data.XmlWriter"></div>/**
15 * @class Ext.data.XmlWriter
16 * @extends Ext.data.DataWriter
17 * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action via XML.
19 Ext.data.XmlWriter = function(params) {
20 Ext.data.XmlWriter.superclass.constructor.apply(this, arguments);
21 this.tpl = new Ext.XTemplate(this.tpl).compile();
23 Ext.extend(Ext.data.XmlWriter, Ext.data.DataWriter, {
24 <div id="cfg-Ext.data.XmlWriter-root"></div>/**
25 * @cfg {String} root [records] The name of the root element when writing <b>multiple</b> records to the server. Each
26 * xml-record written to the server will be wrapped in an element named after {@link Ext.data.XmlReader#record} property.
29 <?xml version="1.0" encoding="UTF-8"?>
30 <user><first>Barney</first></user>
32 * However, when <b>multiple</b> records are written in a batch-operation, these records must be wrapped in a containing
36 <?xml version="1.0" encoding="UTF-8"?>
38 <first>Barney</first></user>
39 <records><first>Barney</first></user>
42 * Defaults to <tt>records</tt>
45 <div id="cfg-Ext.data.XmlWriter-xmlVersion"></div>/**
46 * @cfg {String} xmlVersion [1.0] The <tt>version</tt> written to header of xml documents.
47 <code><pre><?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
50 <div id="cfg-Ext.data.XmlWriter-xmlEncoding"></div>/**
51 * @cfg {String} xmlEncoding [ISO-8859-15] The <tt>encoding</tt> written to header of xml documents.
52 <code><pre><?xml version="1.0" encoding="ISO-8859-15"?></pre></code>
54 xmlEncoding: 'ISO-8859-15',
55 <div id="cfg-Ext.data.XmlWriter-tpl"></div>/**
56 * @cfg {String} tpl The xml template. Defaults to
58 <?xml version="{version}" encoding="{encoding}"?>
59 <tpl if="{[values.nodes.length>1]}"><{root}}>',
60 <tpl for="records">
63 <{name}>{value}</{name}>
67 <tpl if="{[values.records.length>1]}"></{root}}></tpl>
70 // Break up encoding here in case it's being included by some kind of page that will parse it (eg. PHP)
71 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>',
73 <div id="method-Ext.data.XmlWriter-render"></div>/**
74 * Final action of a write event. Apply the written data-object to params.
75 * @param {String} action [Ext.data.Api.create|read|update|destroy]
76 * @param {Ext.data.Record/Ext.data.Record[]} rs
77 * @param {Object} http params
78 * @param {Object/Object[]} rendered data.
80 render : function(action, rs, params, data) {
81 params.xmlData = this.tpl.applyTemplate({
82 version: this.xmlVersion,
83 encoding: this.xmlEncoding,
84 record: this.meta.record,
86 records: (Ext.isArray(rs)) ? data : [data]
91 * Converts an Ext.data.Record to xml
92 * @param {Ext.data.Record} rec
93 * @return {String} rendered xml-element
96 toXml : function(data) {
98 Ext.iterate(data, function(k, v) {
111 * @param {Ext.data.Record} rec
112 * @return {String} xml element
115 createRecord : function(rec) {
116 return this.toXml(this.toHash(rec));
121 * @param {Ext.data.Record} rec
122 * @return {String} xml element
125 updateRecord : function(rec) {
126 return this.toXml(this.toHash(rec));
129 <div id="method-Ext.data.XmlWriter-destroyRecord"></div>/**
131 * @param {Ext.data.Record} rec
132 * @return {String} xml element
134 destroyRecord : function(rec) {
136 data[this.meta.idProperty] = rec.id;
137 return this.toXml(data);