1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.writer.Writer-method-constructor'><span id='Ext-data.writer.Writer'>/**
2 </span></span> * @author Ed Spencer
3 * @class Ext.data.writer.Writer
6 * <p>Base Writer class used by most subclasses of {@link Ext.data.proxy.Server}. This class is
7 * responsible for taking a set of {@link Ext.data.Operation} objects and a {@link Ext.data.Request}
8 * object and modifying that request based on the Operations.</p>
10 * <p>For example a Ext.data.writer.Json would format the Operations and their {@link Ext.data.Model}
11 * instances based on the config options passed to the JsonWriter's constructor.</p>
13 * <p>Writers are not needed for any kind of local storage - whether via a
14 * {@link Ext.data.proxy.WebStorage Web Storage proxy} (see {@link Ext.data.proxy.LocalStorage localStorage}
15 * and {@link Ext.data.proxy.SessionStorage sessionStorage}) or just in memory via a
16 * {@link Ext.data.proxy.Memory MemoryProxy}.</p>
19 * @param {Object} config Optional config object
21 Ext.define('Ext.data.writer.Writer', {
23 alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],
25 <span id='Ext-data.writer.Writer-cfg-writeAllFields'> /**
26 </span> * @cfg {Boolean} writeAllFields True to write all fields from the record to the server. If set to false it
27 * will only send the fields that were modified. Defaults to <tt>true</tt>. Note that any fields that have
28 * {@link Ext.data.Field#persist} set to false will still be ignored.
32 <span id='Ext-data.writer.Writer-cfg-nameProperty'> /**
33 </span> * @cfg {String} nameProperty This property is used to read the key for each value that will be sent to the server.
35 * <pre><code>
36 Ext.define('Person', {
37 extend: 'Ext.data.Model',
48 new Ext.data.writer.Writer({
50 nameProperty: 'mapping'
53 // This will be sent to the server
55 firstName: 'first name value',
56 lastName: 'last name value',
60 * </code></pre>
61 * Defaults to <tt>name</tt>. If the value is not present, the field name will always be used.
65 constructor: function(config) {
66 Ext.apply(this, config);
69 <span id='Ext-data.writer.Writer-method-write'> /**
70 </span> * Prepares a Proxy's Ext.data.Request object
71 * @param {Ext.data.Request} request The request object
72 * @return {Ext.data.Request} The modified request object
74 write: function(request) {
75 var operation = request.operation,
76 records = operation.records || [],
81 for (; i < len; i++) {
82 data.push(this.getRecordData(records[i]));
84 return this.writeRecords(request, data);
87 <span id='Ext-data.writer.Writer-method-getRecordData'> /**
88 </span> * Formats the data for each record before sending it to the server. This
89 * method should be overridden to format the data in a way that differs from the default.
90 * @param {Object} record The record that we are writing to the server.
91 * @return {Object} An object literal of name/value keys to be written to the server.
92 * By default this method returns the data property on the record.
94 getRecordData: function(record) {
95 var isPhantom = record.phantom === true,
96 writeAll = this.writeAllFields || isPhantom,
97 nameProperty = this.nameProperty,
98 fields = record.fields,
106 fields.each(function(field){
108 name = field[nameProperty] || field.name;
109 data[name] = record.get(field.name);
113 // Only write the changes
114 changes = record.getChanges();
115 for (key in changes) {
116 if (changes.hasOwnProperty(key)) {
117 field = fields.get(key);
118 name = field[nameProperty] || field.name;
119 data[name] = changes[key];
123 // always include the id for non phantoms
124 data[record.idProperty] = record.getId();
130 </pre></pre></body></html>