Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / DataWriter.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js"><div id="cls-Ext.data.DataWriter"></div>/**
9  * @class Ext.data.DataWriter
10  * <p>Ext.data.DataWriter facilitates create, update, and destroy actions between
11  * an Ext.data.Store and a server-side framework. A Writer enabled Store will
12  * automatically manage the Ajax requests to perform CRUD actions on a Store.</p>
13  * <p>Ext.data.DataWriter is an abstract base class which is intended to be extended
14  * and should not be created directly. For existing implementations, see
15  * {@link Ext.data.JsonWriter}.</p>
16  * <p>Creating a writer is simple:</p>
17  * <pre><code>
18 var writer = new Ext.data.JsonWriter();
19  * </code></pre>
20  * <p>The proxy for a writer enabled store can be configured with a simple <code>url</code>:</p>
21  * <pre><code>
22 // Create a standard HttpProxy instance.
23 var proxy = new Ext.data.HttpProxy({
24     url: 'app.php/users'
25 });
26  * </code></pre>
27  * <p>For finer grained control, the proxy may also be configured with an <code>api</code>:</p>
28  * <pre><code>
29 // Use the api specification
30 var proxy = new Ext.data.HttpProxy({
31     api: {
32         read    : 'app.php/users/read',
33         create  : 'app.php/users/create',
34         update  : 'app.php/users/update',
35         destroy : 'app.php/users/destroy'
36     }
37 });
38  * </code></pre>
39  * <p>Creating a Writer enabled store:</p>
40  * <pre><code>
41 var store = new Ext.data.Store({
42     proxy: proxy,
43     reader: reader,
44     writer: writer
45 });
46  * </code></pre>
47  * @constructor Create a new DataWriter
48  * @param {Object} meta Metadata configuration options (implementation-specific)
49  * @param {Object} recordType Either an Array of field definition objects as specified
50  * in {@link Ext.data.Record#create}, or an {@link Ext.data.Record} object created
51  * using {@link Ext.data.Record#create}.
52  */
53 Ext.data.DataWriter = function(config){
54     <div id="prop-Ext.data.DataWriter-meta"></div>/**
55      * This DataWriter's configured metadata as passed to the constructor.
56      * @type Mixed
57      * @property meta
58      */
59     Ext.apply(this, config);
60 };
61
62 Ext.data.DataWriter.prototype = {
63
64     <div id="cfg-Ext.data.DataWriter-writeAllFields"></div>/**
65      * @cfg {Boolean} writeAllFields
66      * <tt>false</tt> by default.  Set <tt>true</tt> to have DataWriter return ALL fields of a modified
67      * record -- not just those that changed.
68      * <tt>false</tt> to have DataWriter only request modified fields from a record.
69      */
70     writeAllFields : false,
71     <div id="cfg-Ext.data.DataWriter-listful"></div>/**
72      * @cfg {Boolean} listful
73      * <tt>false</tt> by default.  Set <tt>true</tt> to have the DataWriter <b>always</b> write HTTP params as a list,
74      * even when acting upon a single record.
75      */
76     listful : false,    // <-- listful is actually not used internally here in DataWriter.  @see Ext.data.Store#execute.
77
78     <div id="method-Ext.data.DataWriter-write"></div>/**
79      * Writes data in preparation for server-write action.  Simply proxies to DataWriter#update, DataWriter#create
80      * DataWriter#destroy.
81      * @param {String} action [CREATE|UPDATE|DESTROY]
82      * @param {Object} params The params-hash to write-to
83      * @param {Record/Record[]} rs The recordset write.
84      */
85     write : function(action, params, rs) {
86         this.render(action, rs, params, this[action](rs));
87     },
88
89     <div id="method-Ext.data.DataWriter-render"></div>/**
90      * abstract method meant to be overridden by all DataWriter extensions.  It's the extension's job to apply the "data" to the "params".
91      * The data-object provided to render is populated with data according to the meta-info defined in the user's DataReader config,
92      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
93      * @param {Record[]} rs Store recordset
94      * @param {Object} params Http params to be sent to server.
95      * @param {Object} data object populated according to DataReader meta-data.
96      */
97     render : Ext.emptyFn,
98
99     /**
100      * update
101      * @param {Object} p Params-hash to apply result to.
102      * @param {Record/Record[]} rs Record(s) to write
103      * @private
104      */
105     update : function(rs) {
106         var params = {};
107         if (Ext.isArray(rs)) {
108             var data = [],
109                 ids = [];
110             Ext.each(rs, function(val){
111                 ids.push(val.id);
112                 data.push(this.updateRecord(val));
113             }, this);
114             params[this.meta.idProperty] = ids;
115             params[this.meta.root] = data;
116         }
117         else if (rs instanceof Ext.data.Record) {
118             //params[this.meta.idProperty] = rs.id; // <-- removed, un-neccessary.
119             params[this.meta.root] = this.updateRecord(rs);
120         }
121         return params;
122     },
123
124     <div id="cfg-Ext.data.DataWriter-saveRecord"></div>/**
125      * @cfg {Function} saveRecord Abstract method that should be implemented in all subclasses
126      * (e.g.: {@link Ext.data.JsonWriter#saveRecord JsonWriter.saveRecord}
127      */
128     updateRecord : Ext.emptyFn,
129
130     /**
131      * create
132      * @param {Object} p Params-hash to apply result to.
133      * @param {Record/Record[]} rs Record(s) to write
134      * @private
135      */
136     create : function(rs) {
137         var params = {};
138         if (Ext.isArray(rs)) {
139             var data = [];
140             Ext.each(rs, function(val){
141                 data.push(this.createRecord(val));
142             }, this);
143             params[this.meta.root] = data;
144         }
145         else if (rs instanceof Ext.data.Record) {
146             params[this.meta.root] = this.createRecord(rs);
147         }
148         return params;
149     },
150
151     <div id="cfg-Ext.data.DataWriter-createRecord"></div>/**
152      * @cfg {Function} createRecord Abstract method that should be implemented in all subclasses
153      * (e.g.: {@link Ext.data.JsonWriter#createRecord JsonWriter.createRecord})
154      */
155     createRecord : Ext.emptyFn,
156
157     /**
158      * destroy
159      * @param {Object} p Params-hash to apply result to.
160      * @param {Record/Record[]} rs Record(s) to write
161      * @private
162      */
163     destroy : function(rs) {
164         var params = {};
165         if (Ext.isArray(rs)) {
166             var data = [],
167                 ids = [];
168             Ext.each(rs, function(val){
169                 data.push(this.destroyRecord(val));
170             }, this);
171             params[this.meta.root] = data;
172         } else if (rs instanceof Ext.data.Record) {
173             params[this.meta.root] = this.destroyRecord(rs);
174         }
175         return params;
176     },
177
178     <div id="cfg-Ext.data.DataWriter-destroyRecord"></div>/**
179      * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses
180      * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord})
181      */
182     destroyRecord : Ext.emptyFn,
183
184     /**
185      * Converts a Record to a hash
186      * @param {Record}
187      * @private
188      */
189     toHash : function(rec) {
190         var map = rec.fields.map,
191             data = {},
192             raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
193             m;
194         Ext.iterate(raw, function(prop, value){
195             if((m = map[prop])){
196                 data[m.mapping ? m.mapping : m.name] = value;
197             }
198         });
199         data[this.meta.idProperty] = rec.id;
200         return data;
201     }
202 };</pre>    \r
203 </body>\r
204 </html>