3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.data.DataWriter"></div>/**
15 * @class Ext.data.DataWriter
16 * <p>Ext.data.DataWriter facilitates create, update, and destroy actions between
17 * an Ext.data.Store and a server-side framework. A Writer enabled Store will
18 * automatically manage the Ajax requests to perform CRUD actions on a Store.</p>
19 * <p>Ext.data.DataWriter is an abstract base class which is intended to be extended
20 * and should not be created directly. For existing implementations, see
21 * {@link Ext.data.JsonWriter}.</p>
22 * <p>Creating a writer is simple:</p>
24 var writer = new Ext.data.JsonWriter();
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({
33 * <p>For finer grained control, the proxy may also be configured with an <code>api</code>:</p>
35 // Use the api specification
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 : 'app.php/users/destroy'
45 * <p>Creating a Writer enabled store:</p>
47 var store = new Ext.data.Store({
53 * @constructor Create a new DataWriter
54 * @param {Object} meta Metadata configuration options (implementation-specific)
55 * @param {Object} recordType Either an Array of field definition objects as specified
56 * in {@link Ext.data.Record#create}, or an {@link Ext.data.Record} object created
57 * using {@link Ext.data.Record#create}.
59 Ext.data.DataWriter = function(config){
60 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) {
87 renderer = action + 'Record';
88 // TODO implement @cfg listful here
89 if (Ext.isArray(rs)) {
90 Ext.each(rs, function(rec){
91 data.push(this[renderer](rec));
94 else if (rs instanceof Ext.data.Record) {
95 data = this[renderer](rs);
97 this.render(action, rs, params, data);
100 <div id="method-Ext.data.DataWriter-render"></div>/**
101 * abstract method meant to be overridden by all DataWriter extensions. It's the extension's job to apply the "data" to the "params".
102 * The data-object provided to render is populated with data according to the meta-info defined in the user's DataReader config,
103 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
104 * @param {Record[]} rs Store recordset
105 * @param {Object} params Http params to be sent to server.
106 * @param {Object} data object populated according to DataReader meta-data.
108 render : Ext.emptyFn,
110 <div id="cfg-Ext.data.DataWriter-updateRecord"></div>/**
111 * @cfg {Function} updateRecord Abstract method that should be implemented in all subclasses
112 * (e.g.: {@link Ext.data.JsonWriter#updateRecord JsonWriter.updateRecord}
114 updateRecord : Ext.emptyFn,
116 <div id="cfg-Ext.data.DataWriter-createRecord"></div>/**
117 * @cfg {Function} createRecord Abstract method that should be implemented in all subclasses
118 * (e.g.: {@link Ext.data.JsonWriter#createRecord JsonWriter.createRecord})
120 createRecord : Ext.emptyFn,
122 <div id="cfg-Ext.data.DataWriter-destroyRecord"></div>/**
123 * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses
124 * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord})
126 destroyRecord : Ext.emptyFn,
129 * Converts a Record to a hash.
133 toHash : function(rec) {
134 var map = rec.fields.map,
136 raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
138 Ext.iterate(raw, function(prop, value){
140 data[m.mapping ? m.mapping : m.name] = value;
143 // 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.
144 // We can tell its not auto-increment if the user defined a DataReader field for it *and* that field's value is non-empty.
145 // we could also do a RegExp here for the Ext.data.Record AUTO_ID prefix.
147 if (rec.fields.containsKey(this.meta.idProperty) && Ext.isEmpty(rec.data[this.meta.idProperty])) {
148 delete data[this.meta.idProperty];
151 data[this.meta.idProperty] = rec.id