4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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
21 * Base Writer class used by most subclasses of {@link Ext.data.proxy.Server}. This class is responsible for taking a
22 * set of {@link Ext.data.Operation} objects and a {@link Ext.data.Request} object and modifying that request based on
25 * For example a Ext.data.writer.Json would format the Operations and their {@link Ext.data.Model} instances based on
26 * the config options passed to the JsonWriter's constructor.
28 * Writers are not needed for any kind of local storage - whether via a {@link Ext.data.proxy.WebStorage Web Storage
29 * proxy} (see {@link Ext.data.proxy.LocalStorage localStorage} and {@link Ext.data.proxy.SessionStorage
30 * sessionStorage}) or just in memory via a {@link Ext.data.proxy.Memory MemoryProxy}.
32 Ext.define('Ext.data.writer.Writer', {
34 alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],
36 <span id='Ext-data-writer-Writer-cfg-writeAllFields'> /**
37 </span> * @cfg {Boolean} writeAllFields
38 * True to write all fields from the record to the server. If set to false it will only send the fields that were
39 * modified. Note that any fields that have {@link Ext.data.Field#persist} set to false will still be ignored.
43 <span id='Ext-data-writer-Writer-cfg-nameProperty'> /**
44 </span> * @cfg {String} nameProperty
45 * This property is used to read the key for each value that will be sent to the server. For example:
47 * Ext.define('Person', {
48 * extend: 'Ext.data.Model',
51 * mapping: 'firstName'
59 * new Ext.data.writer.Writer({
60 * writeAllFields: true,
61 * nameProperty: 'mapping'
64 * // This will be sent to the server
66 * firstName: 'first name value',
67 * lastName: 'last name value',
71 * If the value is not present, the field name will always be used.
75 <span id='Ext-data-writer-Writer-method-constructor'> /**
76 </span> * Creates new Writer.
77 * @param {Object} [config] Config object.
79 constructor: function(config) {
80 Ext.apply(this, config);
83 <span id='Ext-data-writer-Writer-method-write'> /**
84 </span> * Prepares a Proxy's Ext.data.Request object
85 * @param {Ext.data.Request} request The request object
86 * @return {Ext.data.Request} The modified request object
88 write: function(request) {
89 var operation = request.operation,
90 records = operation.records || [],
95 for (; i < len; i++) {
96 data.push(this.getRecordData(records[i]));
98 return this.writeRecords(request, data);
101 <span id='Ext-data-writer-Writer-method-getRecordData'> /**
102 </span> * Formats the data for each record before sending it to the server. This method should be overridden to format the
103 * data in a way that differs from the default.
104 * @param {Object} record The record that we are writing to the server.
105 * @return {Object} An object literal of name/value keys to be written to the server. By default this method returns
106 * the data property on the record.
108 getRecordData: function(record) {
109 var isPhantom = record.phantom === true,
110 writeAll = this.writeAllFields || isPhantom,
111 nameProperty = this.nameProperty,
112 fields = record.fields,
120 fields.each(function(field){
122 name = field[nameProperty] || field.name;
123 data[name] = record.get(field.name);
127 // Only write the changes
128 changes = record.getChanges();
129 for (key in changes) {
130 if (changes.hasOwnProperty(key)) {
131 field = fields.get(key);
132 name = field[nameProperty] || field.name;
133 data[name] = changes[key];
137 // always include the id for non phantoms
138 data[record.idProperty] = record.getId();