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
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>
18 var writer = new Ext.data.JsonWriter();
20 * <p>The proxy for a writer enabled store can be configured with a simple <code>url</code>:</p>
22 // Create a standard HttpProxy instance.
23 var proxy = new Ext.data.HttpProxy({
27 * <p>For finer grained control, the proxy may also be configured with an <code>api</code>:</p>
29 // Use the api specification
30 var proxy = new Ext.data.HttpProxy({
32 read : 'app.php/users/read',
33 create : 'app.php/users/create',
34 update : 'app.php/users/update',
35 destroy : 'app.php/users/destroy'
39 * <p>Creating a Writer enabled store:</p>
41 var store = new Ext.data.Store({
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}.
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.
59 Ext.apply(this, config);
62 Ext.data.DataWriter.prototype = {
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.
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.
76 listful : false, // <-- listful is actually not used internally here in DataWriter. @see Ext.data.Store#execute.
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
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.
85 write : function(action, params, rs) {
86 this.render(action, rs, params, this[action](rs));
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.
101 * @param {Object} p Params-hash to apply result to.
102 * @param {Record/Record[]} rs Record(s) to write
105 update : function(rs) {
107 if (Ext.isArray(rs)) {
110 Ext.each(rs, function(val){
112 data.push(this.updateRecord(val));
114 params[this.meta.idProperty] = ids;
115 params[this.meta.root] = data;
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);
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}
128 updateRecord : Ext.emptyFn,
132 * @param {Object} p Params-hash to apply result to.
133 * @param {Record/Record[]} rs Record(s) to write
136 create : function(rs) {
138 if (Ext.isArray(rs)) {
140 Ext.each(rs, function(val){
141 data.push(this.createRecord(val));
143 params[this.meta.root] = data;
145 else if (rs instanceof Ext.data.Record) {
146 params[this.meta.root] = this.createRecord(rs);
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})
155 createRecord : Ext.emptyFn,
159 * @param {Object} p Params-hash to apply result to.
160 * @param {Record/Record[]} rs Record(s) to write
163 destroy : function(rs) {
165 if (Ext.isArray(rs)) {
168 Ext.each(rs, function(val){
169 data.push(this.destroyRecord(val));
171 params[this.meta.root] = data;
172 } else if (rs instanceof Ext.data.Record) {
173 params[this.meta.root] = this.destroyRecord(rs);
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})
182 destroyRecord : Ext.emptyFn,
185 * Converts a Record to a hash
189 toHash : function(rec) {
190 var map = rec.fields.map,
192 raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
194 Ext.iterate(raw, function(prop, value){
196 data[m.mapping ? m.mapping : m.name] = value;
199 data[this.meta.idProperty] = rec.id;