Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / Writer.html
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="../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; }
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  * @class Ext.data.writer.Writer
21  * @extends Object
22  * 
23  * &lt;p&gt;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.&lt;/p&gt;
26  * 
27  * &lt;p&gt;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.&lt;/p&gt;
29  * 
30  * &lt;p&gt;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}.&lt;/p&gt;
34  */
35 Ext.define('Ext.data.writer.Writer', {
36     alias: 'writer.base',
37     alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],
38     
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 &lt;tt&gt;true&lt;/tt&gt;. Note that any fields that have
42      * {@link Ext.data.Field#persist} set to false will still be ignored.
43      */
44     writeAllFields: true,
45     
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.
48      * For example:
49      * &lt;pre&gt;&lt;code&gt;
50 Ext.define('Person', {
51     extend: 'Ext.data.Model',
52     fields: [{
53         name: 'first',
54         mapping: 'firstName'
55     }, {
56         name: 'last',
57         mapping: 'lastName'
58     }, {
59         name: 'age'
60     }]
61 });
62 new Ext.data.writer.Writer({
63     writeAllFields: true,
64     nameProperty: 'mapping'
65 });
66
67 // This will be sent to the server
68 {
69     firstName: 'first name value',
70     lastName: 'last name value',
71     age: 1
72 }
73
74      * &lt;/code&gt;&lt;/pre&gt;
75      * Defaults to &lt;tt&gt;name&lt;/tt&gt;. If the value is not present, the field name will always be used.
76      */
77     nameProperty: 'name',
78
79 <span id='Ext-data-writer-Writer-method-constructor'>    /**
80 </span>     * Creates new Writer.
81      * @param {Object} config (optional) Config object.
82      */
83     constructor: function(config) {
84         Ext.apply(this, config);
85     },
86
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
91      */
92     write: function(request) {
93         var operation = request.operation,
94             records   = operation.records || [],
95             len       = records.length,
96             i         = 0,
97             data      = [];
98
99         for (; i &lt; len; i++) {
100             data.push(this.getRecordData(records[i]));
101         }
102         return this.writeRecords(request, data);
103     },
104
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.
111      */
112     getRecordData: function(record) {
113         var isPhantom = record.phantom === true,
114             writeAll = this.writeAllFields || isPhantom,
115             nameProperty = this.nameProperty,
116             fields = record.fields,
117             data = {},
118             changes,
119             name,
120             field,
121             key;
122         
123         if (writeAll) {
124             fields.each(function(field){
125                 if (field.persist) {
126                     name = field[nameProperty] || field.name;
127                     data[name] = record.get(field.name);
128                 }
129             });
130         } else {
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];
138                 }
139             }
140             if (!isPhantom) {
141                 // always include the id for non phantoms
142                 data[record.idProperty] = record.getId();
143             }
144         }
145         return data;
146     }
147 });
148 </pre>
149 </body>
150 </html>