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'>/**
19 </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>
35 Ext.define('Ext.data.writer.Writer', {
37 alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],
39 <span id='Ext-data-writer-Writer-cfg-writeAllFields'> /**
40 </span> * @cfg {Boolean} writeAllFields True to write all fields from the record to the server. If set to false it
41 * will only send the fields that were modified. Defaults to <tt>true</tt>. Note that any fields that have
42 * {@link Ext.data.Field#persist} set to false will still be ignored.
46 <span id='Ext-data-writer-Writer-cfg-nameProperty'> /**
47 </span> * @cfg {String} nameProperty This property is used to read the key for each value that will be sent to the server.
49 * <pre><code>
50 Ext.define('Person', {
51 extend: 'Ext.data.Model',
62 new Ext.data.writer.Writer({
64 nameProperty: 'mapping'
67 // This will be sent to the server
69 firstName: 'first name value',
70 lastName: 'last name value',
74 * </code></pre>
75 * Defaults to <tt>name</tt>. If the value is not present, the field name will always be used.
79 <span id='Ext-data-writer-Writer-method-constructor'> /**
80 </span> * Creates new Writer.
81 * @param {Object} config (optional) Config object.
83 constructor: function(config) {
84 Ext.apply(this, config);
87 <span id='Ext-data-writer-Writer-method-write'> /**
88 </span> * Prepares a Proxy's Ext.data.Request object
89 * @param {Ext.data.Request} request The request object
90 * @return {Ext.data.Request} The modified request object
92 write: function(request) {
93 var operation = request.operation,
94 records = operation.records || [],
99 for (; i < len; i++) {
100 data.push(this.getRecordData(records[i]));
102 return this.writeRecords(request, data);
105 <span id='Ext-data-writer-Writer-method-getRecordData'> /**
106 </span> * Formats the data for each record before sending it to the server. This
107 * method should be overridden to format the data in a way that differs from the default.
108 * @param {Object} record The record that we are writing to the server.
109 * @return {Object} An object literal of name/value keys to be written to the server.
110 * By default this method returns the data property on the record.
112 getRecordData: function(record) {
113 var isPhantom = record.phantom === true,
114 writeAll = this.writeAllFields || isPhantom,
115 nameProperty = this.nameProperty,
116 fields = record.fields,
124 fields.each(function(field){
126 name = field[nameProperty] || field.name;
127 data[name] = record.get(field.name);
131 // Only write the changes
132 changes = record.getChanges();
133 for (key in changes) {
134 if (changes.hasOwnProperty(key)) {
135 field = fields.get(key);
136 name = field[nameProperty] || field.name;
137 data[name] = changes[key];
141 // always include the id for non phantoms
142 data[record.idProperty] = record.getId();