3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.data.DataWriter"></div>/**
10 * @class Ext.data.DataWriter
11 * <p>Ext.data.DataWriter facilitates create, update, and destroy actions between
12 * an Ext.data.Store and a server-side framework. A Writer enabled Store will
13 * automatically manage the Ajax requests to perform CRUD actions on a Store.</p>
14 * <p>Ext.data.DataWriter is an abstract base class which is intended to be extended
15 * and should not be created directly. For existing implementations, see
16 * {@link Ext.data.JsonWriter}.</p>
17 * <p>Creating a writer is simple:</p>
19 var writer = new Ext.data.JsonWriter({
20 encode: false // <--- false causes data to be printed to jsonData config-property of Ext.Ajax#reqeust
23 * * <p>Same old JsonReader as Ext-2.x:</p>
25 var reader = new Ext.data.JsonReader({idProperty: 'id'}, [{name: 'first'}, {name: 'last'}, {name: 'email'}]);
28 * <p>The proxy for a writer enabled store can be configured with a simple <code>url</code>:</p>
30 // Create a standard HttpProxy instance.
31 var proxy = new Ext.data.HttpProxy({
32 url: 'app.php/users' // <--- Supports "provides"-type urls, such as '/users.json', '/products.xml' (Hello Rails/Merb)
35 * <p>For finer grained control, the proxy may also be configured with an <code>API</code>:</p>
37 // Maximum flexibility with the API-configuration
38 var proxy = new Ext.data.HttpProxy({
40 read : 'app.php/users/read',
41 create : 'app.php/users/create',
42 update : 'app.php/users/update',
43 destroy : { // <--- Supports object-syntax as well
44 url: 'app.php/users/destroy',
50 * <p>Pulling it all together into a Writer-enabled Store:</p>
52 var store = new Ext.data.Store({
57 autoSave: true // -- Cell-level updates.
60 * <p>Initiating write-actions <b>automatically</b>, using the existing Ext2.0 Store/Record API:</p>
62 var rec = store.getAt(0);
63 rec.set('email', 'foo@bar.com'); // <--- Immediately initiates an UPDATE action through configured proxy.
65 store.remove(rec); // <---- Immediately initiates a DESTROY action through configured proxy.
67 * <p>For <b>record/batch</b> updates, use the Store-configuration {@link Ext.data.Store#autoSave autoSave:false}</p>
69 var store = new Ext.data.Store({
74 autoSave: false // -- disable cell-updates
77 var urec = store.getAt(0);
78 urec.set('email', 'foo@bar.com');
80 var drec = store.getAt(1);
86 * @constructor Create a new DataWriter
87 * @param {Object} meta Metadata configuration options (implementation-specific)
88 * @param {Object} recordType Either an Array of field definition objects as specified
89 * in {@link Ext.data.Record#create}, or an {@link Ext.data.Record} object created
90 * using {@link Ext.data.Record#create}.
92 Ext.data.DataWriter = function(config){
93 Ext.apply(this, config);
95 Ext.data.DataWriter.prototype = {
97 <div id="cfg-Ext.data.DataWriter-writeAllFields"></div>/**
98 * @cfg {Boolean} writeAllFields
99 * <tt>false</tt> by default. Set <tt>true</tt> to have DataWriter return ALL fields of a modified
100 * record -- not just those that changed.
101 * <tt>false</tt> to have DataWriter only request modified fields from a record.
103 writeAllFields : false,
104 <div id="cfg-Ext.data.DataWriter-listful"></div>/**
105 * @cfg {Boolean} listful
106 * <tt>false</tt> by default. Set <tt>true</tt> to have the DataWriter <b>always</b> write HTTP params as a list,
107 * even when acting upon a single record.
109 listful : false, // <-- listful is actually not used internally here in DataWriter. @see Ext.data.Store#execute.
111 <div id="method-Ext.data.DataWriter-apply"></div>/**
112 * 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},
113 * Where the first parameter is the <i>receiver</i> of paramaters and the second, baseParams, <i>the source</i>.
114 * @param {Object} params The request-params receiver.
115 * @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}.
116 * @param {String} action [{@link Ext.data.Api#actions create|update|destroy}]
117 * @param {Record/Record[]} rs The recordset to write, the subject(s) of the write action.
119 apply : function(params, baseParams, action, rs) {
121 renderer = action + 'Record';
122 // TODO implement @cfg listful here
123 if (Ext.isArray(rs)) {
124 Ext.each(rs, function(rec){
125 data.push(this[renderer](rec));
128 else if (rs instanceof Ext.data.Record) {
129 data = this[renderer](rs);
131 this.render(params, baseParams, data);
134 <div id="method-Ext.data.DataWriter-render"></div>/**
135 * abstract method meant to be overridden by all DataWriter extensions. It's the extension's job to apply the "data" to the "params".
136 * The data-object provided to render is populated with data according to the meta-info defined in the user's DataReader config,
137 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
138 * @param {Record[]} rs Store recordset
139 * @param {Object} params Http params to be sent to server.
140 * @param {Object} data object populated according to DataReader meta-data.
142 render : Ext.emptyFn,
144 <div id="cfg-Ext.data.DataWriter-updateRecord"></div>/**
145 * @cfg {Function} updateRecord Abstract method that should be implemented in all subclasses
146 * (e.g.: {@link Ext.data.JsonWriter#updateRecord JsonWriter.updateRecord}
148 updateRecord : Ext.emptyFn,
150 <div id="cfg-Ext.data.DataWriter-createRecord"></div>/**
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,
156 <div id="cfg-Ext.data.DataWriter-destroyRecord"></div>/**
157 * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses
158 * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord})
160 destroyRecord : Ext.emptyFn,
162 <div id="method-Ext.data.DataWriter-toHash"></div>/**
163 * Converts a Record to a hash, taking into account the state of the Ext.data.Record along with configuration properties
164 * related to its rendering, such as {@link #writeAllFields}, {@link Ext.data.Record#phantom phantom}, {@link Ext.data.Record#getChanges getChanges} and
165 * {@link Ext.data.DataReader#idProperty idProperty}
166 * @param {Ext.data.Record}
167 * @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.
170 * TODO Implement excludes/only configuration with 2nd param?
172 toHash : function(rec, config) {
173 var map = rec.fields.map,
175 raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
177 Ext.iterate(raw, function(prop, value){
179 data[m.mapping ? m.mapping : m.name] = value;
182 // 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.
183 // We can tell its not auto-increment if the user defined a DataReader field for it *and* that field's value is non-empty.
184 // we could also do a RegExp here for the Ext.data.Record AUTO_ID prefix.
186 if (rec.fields.containsKey(this.meta.idProperty) && Ext.isEmpty(rec.data[this.meta.idProperty])) {
187 delete data[this.meta.idProperty];
190 data[this.meta.idProperty] = rec.id
195 <div id="method-Ext.data.DataWriter-toArray"></div>/**
196 * Converts a {@link Ext.data.DataWriter#toHash Hashed} {@link Ext.data.Record} to fields-array array suitable
197 * for encoding to xml via XTemplate, eg:
198 <code><pre><tpl for="."><{name}>{value}</{name}</tpl></pre></code>
199 * eg, <b>non-phantom</b>:
200 <code><pre>{id: 1, first: 'foo', last: 'bar'} --> [{name: 'id', value: 1}, {name: 'first', value: 'foo'}, {name: 'last', value: 'bar'}]</pre></code>
201 * {@link Ext.data.Record#phantom Phantom} records will have had their idProperty omitted in {@link #toHash} if determined to be auto-generated.
202 * Non AUTOINCREMENT pks should have been protected.
203 * @param {Hash} data Hashed by Ext.data.DataWriter#toHash
204 * @return {[Object]} Array of attribute-objects.
207 toArray : function(data) {
209 Ext.iterate(data, function(k, v) {fields.push({name: k, value: v});},this);