3 * Copyright(c) 2006-2010 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.data.DataWriter
9 * <p>Ext.data.DataWriter facilitates create, update, and destroy actions between
10 * an Ext.data.Store and a server-side framework. A Writer enabled Store will
11 * automatically manage the Ajax requests to perform CRUD actions on a Store.</p>
12 * <p>Ext.data.DataWriter is an abstract base class which is intended to be extended
13 * and should not be created directly. For existing implementations, see
14 * {@link Ext.data.JsonWriter}.</p>
15 * <p>Creating a writer is simple:</p>
17 var writer = new Ext.data.JsonWriter({
18 encode: false // <--- false causes data to be printed to jsonData config-property of Ext.Ajax#reqeust
21 * * <p>Same old JsonReader as Ext-2.x:</p>
23 var reader = new Ext.data.JsonReader({idProperty: 'id'}, [{name: 'first'}, {name: 'last'}, {name: 'email'}]);
26 * <p>The proxy for a writer enabled store can be configured with a simple <code>url</code>:</p>
28 // Create a standard HttpProxy instance.
29 var proxy = new Ext.data.HttpProxy({
30 url: 'app.php/users' // <--- Supports "provides"-type urls, such as '/users.json', '/products.xml' (Hello Rails/Merb)
33 * <p>For finer grained control, the proxy may also be configured with an <code>API</code>:</p>
35 // Maximum flexibility with the API-configuration
36 var proxy = new Ext.data.HttpProxy({
38 read : 'app.php/users/read',
39 create : 'app.php/users/create',
40 update : 'app.php/users/update',
41 destroy : { // <--- Supports object-syntax as well
42 url: 'app.php/users/destroy',
48 * <p>Pulling it all together into a Writer-enabled Store:</p>
50 var store = new Ext.data.Store({
55 autoSave: true // -- Cell-level updates.
58 * <p>Initiating write-actions <b>automatically</b>, using the existing Ext2.0 Store/Record API:</p>
60 var rec = store.getAt(0);
61 rec.set('email', 'foo@bar.com'); // <--- Immediately initiates an UPDATE action through configured proxy.
63 store.remove(rec); // <---- Immediately initiates a DESTROY action through configured proxy.
65 * <p>For <b>record/batch</b> updates, use the Store-configuration {@link Ext.data.Store#autoSave autoSave:false}</p>
67 var store = new Ext.data.Store({
72 autoSave: false // -- disable cell-updates
75 var urec = store.getAt(0);
76 urec.set('email', 'foo@bar.com');
78 var drec = store.getAt(1);
84 * @constructor Create a new DataWriter
85 * @param {Object} meta Metadata configuration options (implementation-specific)
86 * @param {Object} recordType Either an Array of field definition objects as specified
87 * in {@link Ext.data.Record#create}, or an {@link Ext.data.Record} object created
88 * using {@link Ext.data.Record#create}.
90 Ext.data.DataWriter = function(config){
91 Ext.apply(this, config);
93 Ext.data.DataWriter.prototype = {
96 * @cfg {Boolean} writeAllFields
97 * <tt>false</tt> by default. Set <tt>true</tt> to have DataWriter return ALL fields of a modified
98 * record -- not just those that changed.
99 * <tt>false</tt> to have DataWriter only request modified fields from a record.
101 writeAllFields : false,
103 * @cfg {Boolean} listful
104 * <tt>false</tt> by default. Set <tt>true</tt> to have the DataWriter <b>always</b> write HTTP params as a list,
105 * even when acting upon a single record.
107 listful : false, // <-- listful is actually not used internally here in DataWriter. @see Ext.data.Store#execute.
110 * Compiles a Store recordset into a data-format defined by an extension such as {@link Ext.data.JsonWriter} or {@link Ext.data.XmlWriter} in preparation for a {@link Ext.data.Api#actions server-write action}. The first two params are similar similar in nature to {@link Ext#apply},
111 * Where the first parameter is the <i>receiver</i> of paramaters and the second, baseParams, <i>the source</i>.
112 * @param {Object} params The request-params receiver.
113 * @param {Object} baseParams as defined by {@link Ext.data.Store#baseParams}. The baseParms must be encoded by the extending class, eg: {@link Ext.data.JsonWriter}, {@link Ext.data.XmlWriter}.
114 * @param {String} action [{@link Ext.data.Api#actions create|update|destroy}]
115 * @param {Record/Record[]} rs The recordset to write, the subject(s) of the write action.
117 apply : function(params, baseParams, action, rs) {
119 renderer = action + 'Record';
120 // TODO implement @cfg listful here
121 if (Ext.isArray(rs)) {
122 Ext.each(rs, function(rec){
123 data.push(this[renderer](rec));
126 else if (rs instanceof Ext.data.Record) {
127 data = this[renderer](rs);
129 this.render(params, baseParams, data);
133 * abstract method meant to be overridden by all DataWriter extensions. It's the extension's job to apply the "data" to the "params".
134 * The data-object provided to render is populated with data according to the meta-info defined in the user's DataReader config,
135 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
136 * @param {Record[]} rs Store recordset
137 * @param {Object} params Http params to be sent to server.
138 * @param {Object} data object populated according to DataReader meta-data.
140 render : Ext.emptyFn,
143 * @cfg {Function} updateRecord Abstract method that should be implemented in all subclasses
144 * (e.g.: {@link Ext.data.JsonWriter#updateRecord JsonWriter.updateRecord}
146 updateRecord : Ext.emptyFn,
149 * @cfg {Function} createRecord Abstract method that should be implemented in all subclasses
150 * (e.g.: {@link Ext.data.JsonWriter#createRecord JsonWriter.createRecord})
152 createRecord : Ext.emptyFn,
155 * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses
156 * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord})
158 destroyRecord : Ext.emptyFn,
161 * Converts a Record to a hash, taking into account the state of the Ext.data.Record along with configuration properties
162 * related to its rendering, such as {@link #writeAllFields}, {@link Ext.data.Record#phantom phantom}, {@link Ext.data.Record#getChanges getChanges} and
163 * {@link Ext.data.DataReader#idProperty idProperty}
164 * @param {Ext.data.Record}
165 * @param {Object} config <b>NOT YET IMPLEMENTED</b>. Will implement an exlude/only configuration for fine-control over which fields do/don't get rendered.
168 * TODO Implement excludes/only configuration with 2nd param?
170 toHash : function(rec, config) {
171 var map = rec.fields.map,
173 raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
175 Ext.iterate(raw, function(prop, value){
177 data[m.mapping ? m.mapping : m.name] = value;
180 // we don't want to write Ext auto-generated id to hash. Careful not to remove it on Models not having auto-increment pk though.
181 // We can tell its not auto-increment if the user defined a DataReader field for it *and* that field's value is non-empty.
182 // we could also do a RegExp here for the Ext.data.Record AUTO_ID prefix.
184 if (rec.fields.containsKey(this.meta.idProperty) && Ext.isEmpty(rec.data[this.meta.idProperty])) {
185 delete data[this.meta.idProperty];
188 data[this.meta.idProperty] = rec.id
194 * Converts a {@link Ext.data.DataWriter#toHash Hashed} {@link Ext.data.Record} to fields-array array suitable
195 * for encoding to xml via XTemplate, eg:
196 <code><pre><tpl for="."><{name}>{value}</{name}</tpl></pre></code>
197 * eg, <b>non-phantom</b>:
198 <code><pre>{id: 1, first: 'foo', last: 'bar'} --> [{name: 'id', value: 1}, {name: 'first', value: 'foo'}, {name: 'last', value: 'bar'}]</pre></code>
199 * {@link Ext.data.Record#phantom Phantom} records will have had their idProperty omitted in {@link #toHash} if determined to be auto-generated.
200 * Non AUTOINCREMENT pks should have been protected.
201 * @param {Hash} data Hashed by Ext.data.DataWriter#toHash
202 * @return {[Object]} Array of attribute-objects.
205 toArray : function(data) {
207 Ext.iterate(data, function(k, v) {fields.push({name: k, value: v});},this);