3 * Copyright(c) 2006-2009 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();
19 * <p>The proxy for a writer enabled store can be configured with a simple <code>url</code>:</p>
21 // Create a standard HttpProxy instance.
22 var proxy = new Ext.data.HttpProxy({
26 * <p>For finer grained control, the proxy may also be configured with an <code>api</code>:</p>
28 // Use the api specification
29 var proxy = new Ext.data.HttpProxy({
31 read : 'app.php/users/read',
32 create : 'app.php/users/create',
33 update : 'app.php/users/update',
34 destroy : 'app.php/users/destroy'
38 * <p>Creating a Writer enabled store:</p>
40 var store = new Ext.data.Store({
46 * @constructor Create a new DataWriter
47 * @param {Object} meta Metadata configuration options (implementation-specific)
48 * @param {Object} recordType Either an Array of field definition objects as specified
49 * in {@link Ext.data.Record#create}, or an {@link Ext.data.Record} object created
50 * using {@link Ext.data.Record#create}.
52 Ext.data.DataWriter = function(config){
54 * This DataWriter's configured metadata as passed to the constructor.
58 Ext.apply(this, config);
61 Ext.data.DataWriter.prototype = {
64 * @cfg {Boolean} writeAllFields
65 * <tt>false</tt> by default. Set <tt>true</tt> to have DataWriter return ALL fields of a modified
66 * record -- not just those that changed.
67 * <tt>false</tt> to have DataWriter only request modified fields from a record.
69 writeAllFields : false,
71 * @cfg {Boolean} listful
72 * <tt>false</tt> by default. Set <tt>true</tt> to have the DataWriter <b>always</b> write HTTP params as a list,
73 * even when acting upon a single record.
75 listful : false, // <-- listful is actually not used internally here in DataWriter. @see Ext.data.Store#execute.
78 * Writes data in preparation for server-write action. Simply proxies to DataWriter#update, DataWriter#create
80 * @param {String} action [CREATE|UPDATE|DESTROY]
81 * @param {Object} params The params-hash to write-to
82 * @param {Record/Record[]} rs The recordset write.
84 write : function(action, params, rs) {
85 this.render(action, rs, params, this[action](rs));
89 * abstract method meant to be overridden by all DataWriter extensions. It's the extension's job to apply the "data" to the "params".
90 * The data-object provided to render is populated with data according to the meta-info defined in the user's DataReader config,
91 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
92 * @param {Record[]} rs Store recordset
93 * @param {Object} params Http params to be sent to server.
94 * @param {Object} data object populated according to DataReader meta-data.
100 * @param {Object} p Params-hash to apply result to.
101 * @param {Record/Record[]} rs Record(s) to write
104 update : function(rs) {
106 if (Ext.isArray(rs)) {
109 Ext.each(rs, function(val){
111 data.push(this.updateRecord(val));
113 params[this.meta.idProperty] = ids;
114 params[this.meta.root] = data;
116 else if (rs instanceof Ext.data.Record) {
117 params[this.meta.idProperty] = rs.id;
118 params[this.meta.root] = this.updateRecord(rs);
124 * @cfg {Function} saveRecord Abstract method that should be implemented in all subclasses
125 * (e.g.: {@link Ext.data.JsonWriter#saveRecord JsonWriter.saveRecord}
127 updateRecord : Ext.emptyFn,
131 * @param {Object} p Params-hash to apply result to.
132 * @param {Record/Record[]} rs Record(s) to write
135 create : function(rs) {
137 if (Ext.isArray(rs)) {
139 Ext.each(rs, function(val){
140 data.push(this.createRecord(val));
142 params[this.meta.root] = data;
144 else if (rs instanceof Ext.data.Record) {
145 params[this.meta.root] = this.createRecord(rs);
151 * @cfg {Function} createRecord Abstract method that should be implemented in all subclasses
152 * (e.g.: {@link Ext.data.JsonWriter#createRecord JsonWriter.createRecord})
154 createRecord : Ext.emptyFn,
158 * @param {Object} p Params-hash to apply result to.
159 * @param {Record/Record[]} rs Record(s) to write
162 destroy : function(rs) {
164 if (Ext.isArray(rs)) {
167 Ext.each(rs, function(val){
168 data.push(this.destroyRecord(val));
170 params[this.meta.root] = data;
171 } else if (rs instanceof Ext.data.Record) {
172 params[this.meta.root] = this.destroyRecord(rs);
178 * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses
179 * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord})
181 destroyRecord : Ext.emptyFn,
184 * Converts a Record to a hash
188 toHash : function(rec) {
189 var map = rec.fields.map,
191 raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
193 Ext.iterate(raw, function(prop, value){
195 data[m.mapping ? m.mapping : m.name] = value;
198 data[this.meta.idProperty] = rec.id;