[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 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> |
---|
| 16 | * <pre><code> |
---|
| 17 | var writer = new Ext.data.JsonWriter({ |
---|
| 18 | encode: false // <--- false causes data to be printed to jsonData config-property of Ext.Ajax#reqeust |
---|
| 19 | }); |
---|
| 20 | * </code></pre> |
---|
| 21 | * * <p>Same old JsonReader as Ext-2.x:</p> |
---|
| 22 | * <pre><code> |
---|
| 23 | var reader = new Ext.data.JsonReader({idProperty: 'id'}, [{name: 'first'}, {name: 'last'}, {name: 'email'}]); |
---|
| 24 | * </code></pre> |
---|
| 25 | * |
---|
| 26 | * <p>The proxy for a writer enabled store can be configured with a simple <code>url</code>:</p> |
---|
| 27 | * <pre><code> |
---|
| 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) |
---|
| 31 | }); |
---|
| 32 | * </code></pre> |
---|
| 33 | * <p>For finer grained control, the proxy may also be configured with an <code>API</code>:</p> |
---|
| 34 | * <pre><code> |
---|
| 35 | // Maximum flexibility with the API-configuration |
---|
| 36 | var proxy = new Ext.data.HttpProxy({ |
---|
| 37 | api: { |
---|
| 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', |
---|
| 43 | method: "DELETE" |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | }); |
---|
| 47 | * </code></pre> |
---|
| 48 | * <p>Pulling it all together into a Writer-enabled Store:</p> |
---|
| 49 | * <pre><code> |
---|
| 50 | var store = new Ext.data.Store({ |
---|
| 51 | proxy: proxy, |
---|
| 52 | reader: reader, |
---|
| 53 | writer: writer, |
---|
| 54 | autoLoad: true, |
---|
| 55 | autoSave: true // -- Cell-level updates. |
---|
| 56 | }); |
---|
| 57 | * </code></pre> |
---|
| 58 | * <p>Initiating write-actions <b>automatically</b>, using the existing Ext2.0 Store/Record API:</p> |
---|
| 59 | * <pre><code> |
---|
| 60 | var rec = store.getAt(0); |
---|
| 61 | rec.set('email', 'foo@bar.com'); // <--- Immediately initiates an UPDATE action through configured proxy. |
---|
| 62 | |
---|
| 63 | store.remove(rec); // <---- Immediately initiates a DESTROY action through configured proxy. |
---|
| 64 | * </code></pre> |
---|
| 65 | * <p>For <b>record/batch</b> updates, use the Store-configuration {@link Ext.data.Store#autoSave autoSave:false}</p> |
---|
| 66 | * <pre><code> |
---|
| 67 | var store = new Ext.data.Store({ |
---|
| 68 | proxy: proxy, |
---|
| 69 | reader: reader, |
---|
| 70 | writer: writer, |
---|
| 71 | autoLoad: true, |
---|
| 72 | autoSave: false // -- disable cell-updates |
---|
| 73 | }); |
---|
| 74 | |
---|
| 75 | var urec = store.getAt(0); |
---|
| 76 | urec.set('email', 'foo@bar.com'); |
---|
| 77 | |
---|
| 78 | var drec = store.getAt(1); |
---|
| 79 | store.remove(drec); |
---|
| 80 | |
---|
| 81 | // Push the button! |
---|
| 82 | store.save(); |
---|
| 83 | * </code></pre> |
---|
| 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}. |
---|
| 89 | */ |
---|
| 90 | Ext.data.DataWriter = function(config){ |
---|
| 91 | Ext.apply(this, config); |
---|
| 92 | }; |
---|
| 93 | Ext.data.DataWriter.prototype = { |
---|
| 94 | |
---|
| 95 | /** |
---|
| 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. |
---|
| 100 | */ |
---|
| 101 | writeAllFields : false, |
---|
| 102 | /** |
---|
| 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. |
---|
| 106 | */ |
---|
| 107 | listful : false, // <-- listful is actually not used internally here in DataWriter. @see Ext.data.Store#execute. |
---|
| 108 | |
---|
| 109 | /** |
---|
| 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. |
---|
| 116 | */ |
---|
| 117 | apply : function(params, baseParams, action, rs) { |
---|
| 118 | var data = [], |
---|
| 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)); |
---|
| 124 | }, this); |
---|
| 125 | } |
---|
| 126 | else if (rs instanceof Ext.data.Record) { |
---|
| 127 | data = this[renderer](rs); |
---|
| 128 | } |
---|
| 129 | this.render(params, baseParams, data); |
---|
| 130 | }, |
---|
| 131 | |
---|
| 132 | /** |
---|
| 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. |
---|
| 139 | */ |
---|
| 140 | render : Ext.emptyFn, |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * @cfg {Function} updateRecord Abstract method that should be implemented in all subclasses |
---|
| 144 | * (e.g.: {@link Ext.data.JsonWriter#updateRecord JsonWriter.updateRecord} |
---|
| 145 | */ |
---|
| 146 | updateRecord : Ext.emptyFn, |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * @cfg {Function} createRecord Abstract method that should be implemented in all subclasses |
---|
| 150 | * (e.g.: {@link Ext.data.JsonWriter#createRecord JsonWriter.createRecord}) |
---|
| 151 | */ |
---|
| 152 | createRecord : Ext.emptyFn, |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * @cfg {Function} destroyRecord Abstract method that should be implemented in all subclasses |
---|
| 156 | * (e.g.: {@link Ext.data.JsonWriter#destroyRecord JsonWriter.destroyRecord}) |
---|
| 157 | */ |
---|
| 158 | destroyRecord : Ext.emptyFn, |
---|
| 159 | |
---|
| 160 | /** |
---|
| 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} rec The Record from which to create a hash. |
---|
| 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. |
---|
| 166 | * @return {Object} |
---|
| 167 | * @protected |
---|
| 168 | * TODO Implement excludes/only configuration with 2nd param? |
---|
| 169 | */ |
---|
| 170 | toHash : function(rec, config) { |
---|
| 171 | var map = rec.fields.map, |
---|
| 172 | data = {}, |
---|
| 173 | raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data, |
---|
| 174 | m; |
---|
| 175 | Ext.iterate(raw, function(prop, value){ |
---|
| 176 | if((m = map[prop])){ |
---|
| 177 | data[m.mapping ? m.mapping : m.name] = value; |
---|
| 178 | } |
---|
| 179 | }); |
---|
| 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. |
---|
| 183 | if (rec.phantom) { |
---|
| 184 | if (rec.fields.containsKey(this.meta.idProperty) && Ext.isEmpty(rec.data[this.meta.idProperty])) { |
---|
| 185 | delete data[this.meta.idProperty]; |
---|
| 186 | } |
---|
| 187 | } else { |
---|
| 188 | data[this.meta.idProperty] = rec.id; |
---|
| 189 | } |
---|
| 190 | return data; |
---|
| 191 | }, |
---|
| 192 | |
---|
| 193 | /** |
---|
| 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. |
---|
| 203 | * @protected |
---|
| 204 | */ |
---|
| 205 | toArray : function(data) { |
---|
| 206 | var fields = []; |
---|
| 207 | Ext.iterate(data, function(k, v) {fields.push({name: k, value: v});},this); |
---|
| 208 | return fields; |
---|
| 209 | } |
---|
| 210 | }; |
---|