/ext-4.0.7/docs/source/Writer.html
HTML | 146 lines | 139 code | 7 blank | 0 comment | 0 complexity | 6c9ecf64913e8d1d58dfc2965f823506 MD5 | raw file
1<!DOCTYPE html>
2<html>
3<head>
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; }
10 </style>
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14 }
15 </script>
16</head>
17<body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-writer-Writer'>/**
19</span> * @author Ed Spencer
20 *
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
23 * the Operations.
24 *
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.
27 *
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}.
31 */
32Ext.define('Ext.data.writer.Writer', {
33 alias: 'writer.base',
34 alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],
35
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.
40 */
41 writeAllFields: true,
42
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:
46 *
47 * Ext.define('Person', {
48 * extend: 'Ext.data.Model',
49 * fields: [{
50 * name: 'first',
51 * mapping: 'firstName'
52 * }, {
53 * name: 'last',
54 * mapping: 'lastName'
55 * }, {
56 * name: 'age'
57 * }]
58 * });
59 * new Ext.data.writer.Writer({
60 * writeAllFields: true,
61 * nameProperty: 'mapping'
62 * });
63 *
64 * // This will be sent to the server
65 * {
66 * firstName: 'first name value',
67 * lastName: 'last name value',
68 * age: 1
69 * }
70 *
71 * If the value is not present, the field name will always be used.
72 */
73 nameProperty: 'name',
74
75<span id='Ext-data-writer-Writer-method-constructor'> /**
76</span> * Creates new Writer.
77 * @param {Object} [config] Config object.
78 */
79 constructor: function(config) {
80 Ext.apply(this, config);
81 },
82
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
87 */
88 write: function(request) {
89 var operation = request.operation,
90 records = operation.records || [],
91 len = records.length,
92 i = 0,
93 data = [];
94
95 for (; i < len; i++) {
96 data.push(this.getRecordData(records[i]));
97 }
98 return this.writeRecords(request, data);
99 },
100
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.
107 */
108 getRecordData: function(record) {
109 var isPhantom = record.phantom === true,
110 writeAll = this.writeAllFields || isPhantom,
111 nameProperty = this.nameProperty,
112 fields = record.fields,
113 data = {},
114 changes,
115 name,
116 field,
117 key;
118
119 if (writeAll) {
120 fields.each(function(field){
121 if (field.persist) {
122 name = field[nameProperty] || field.name;
123 data[name] = record.get(field.name);
124 }
125 });
126 } else {
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];
134 }
135 }
136 if (!isPhantom) {
137 // always include the id for non phantoms
138 data[record.idProperty] = record.getId();
139 }
140 }
141 return data;
142 }
143});
144</pre>
145</body>
146</html>