4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-writer-Writer-method-constructor'><span id='Ext-data-writer-Writer'>/**
19 </span></span> * @author Ed Spencer
20 * @class Ext.data.writer.Writer
23 * <p>Base Writer class used by most subclasses of {@link Ext.data.proxy.Server}. This class is
24 * responsible for taking a set of {@link Ext.data.Operation} objects and a {@link Ext.data.Request}
25 * object and modifying that request based on the Operations.</p>
27 * <p>For example a Ext.data.writer.Json would format the Operations and their {@link Ext.data.Model}
28 * instances based on the config options passed to the JsonWriter's constructor.</p>
30 * <p>Writers are not needed for any kind of local storage - whether via a
31 * {@link Ext.data.proxy.WebStorage Web Storage proxy} (see {@link Ext.data.proxy.LocalStorage localStorage}
32 * and {@link Ext.data.proxy.SessionStorage sessionStorage}) or just in memory via a
33 * {@link Ext.data.proxy.Memory MemoryProxy}.</p>
36 * @param {Object} config Optional config object
38 Ext.define('Ext.data.writer.Writer', {
40 alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],
42 <span id='Ext-data-writer-Writer-cfg-writeAllFields'> /**
43 </span> * @cfg {Boolean} writeAllFields True to write all fields from the record to the server. If set to false it
44 * will only send the fields that were modified. Defaults to <tt>true</tt>. Note that any fields that have
45 * {@link Ext.data.Field#persist} set to false will still be ignored.
49 <span id='Ext-data-writer-Writer-cfg-nameProperty'> /**
50 </span> * @cfg {String} nameProperty This property is used to read the key for each value that will be sent to the server.
52 * <pre><code>
53 Ext.define('Person', {
54 extend: 'Ext.data.Model',
65 new Ext.data.writer.Writer({
67 nameProperty: 'mapping'
70 // This will be sent to the server
72 firstName: 'first name value',
73 lastName: 'last name value',
77 * </code></pre>
78 * Defaults to <tt>name</tt>. If the value is not present, the field name will always be used.
82 constructor: function(config) {
83 Ext.apply(this, config);
86 <span id='Ext-data-writer-Writer-method-write'> /**
87 </span> * Prepares a Proxy's Ext.data.Request object
88 * @param {Ext.data.Request} request The request object
89 * @return {Ext.data.Request} The modified request object
91 write: function(request) {
92 var operation = request.operation,
93 records = operation.records || [],
98 for (; i < len; i++) {
99 data.push(this.getRecordData(records[i]));
101 return this.writeRecords(request, data);
104 <span id='Ext-data-writer-Writer-method-getRecordData'> /**
105 </span> * Formats the data for each record before sending it to the server. This
106 * method should be overridden to format the data in a way that differs from the default.
107 * @param {Object} record The record that we are writing to the server.
108 * @return {Object} An object literal of name/value keys to be written to the server.
109 * By default this method returns the data property on the record.
111 getRecordData: function(record) {
112 var isPhantom = record.phantom === true,
113 writeAll = this.writeAllFields || isPhantom,
114 nameProperty = this.nameProperty,
115 fields = record.fields,
123 fields.each(function(field){
125 name = field[nameProperty] || field.name;
126 data[name] = record.get(field.name);
130 // Only write the changes
131 changes = record.getChanges();
132 for (key in changes) {
133 if (changes.hasOwnProperty(key)) {
134 field = fields.get(key);
135 name = field[nameProperty] || field.name;
136 data[name] = changes[key];
140 // always include the id for non phantoms
141 data[record.idProperty] = record.getId();