3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.data.JsonWriter
9 * @extends Ext.data.DataWriter
10 * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action.
12 Ext.data.JsonWriter = Ext.extend(Ext.data.DataWriter, {
14 * @cfg {Boolean} encode <p><tt>true</tt> to {@link Ext.util.JSON#encode JSON encode} the
15 * {@link Ext.data.DataWriter#toHash hashed data} into a standard HTTP parameter named after this
16 * Reader's <code>meta.root</code> property which, by default is imported from the associated Reader. Defaults to <tt>true</tt>.</p>
17 * <p>If set to <code>false</code>, the hashed data is {@link Ext.util.JSON#encode JSON encoded}, along with
18 * the associated {@link Ext.data.Store}'s {@link Ext.data.Store#baseParams baseParams}, into the POST body.</p>
19 * <p>When using {@link Ext.data.DirectProxy}, set this to <tt>false</tt> since Ext.Direct.JsonProvider will perform
20 * its own json-encoding. In addition, if you're using {@link Ext.data.HttpProxy}, setting to <tt>false</tt>
21 * will cause HttpProxy to transmit data using the <b>jsonData</b> configuration-params of {@link Ext.Ajax#request}
22 * instead of <b>params</b>.</p>
23 * <p>When using a {@link Ext.data.Store#restful} Store, some serverside frameworks are
24 * tuned to expect data through the jsonData mechanism. In those cases, one will want to set <b>encode: <tt>false</tt></b>, as in
25 * let the lower-level connection object (eg: Ext.Ajax) do the encoding.</p>
29 * @cfg {Boolean} encodeDelete False to send only the id to the server on delete, true to encode it in an object
30 * literal, eg: <pre><code>
32 * </code></pre> Defaults to <tt>false</tt>
36 constructor : function(config){
37 Ext.data.JsonWriter.superclass.constructor.call(this, config);
41 * <p>This method should not need to be called by application code, however it may be useful on occasion to
42 * override it, or augment it with an {@link Function#createInterceptor interceptor} or {@link Function#createSequence sequence}.</p>
43 * <p>The provided implementation encodes the serialized data representing the Store's modified Records into the Ajax request's
44 * <code>params</code> according to the <code>{@link #encode}</code> setting.</p>
45 * @param {Object} Ajax request params object to write into.
46 * @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}.
47 * @param {Object/Object[]} data Data object representing the serialized modified records from the Store. May be either a single object,
48 * or an Array of objects - user implementations must handle both cases.
50 render : function(params, baseParams, data) {
51 if (this.encode === true) {
53 Ext.apply(params, baseParams);
54 params[this.meta.root] = Ext.encode(data);
56 // defer encoding for some other layer, probably in {@link Ext.Ajax#request}. Place everything into "jsonData" key.
57 var jdata = Ext.apply({}, baseParams);
58 jdata[this.meta.root] = data;
59 params.jsonData = jdata;
63 * Implements abstract Ext.data.DataWriter#createRecord
65 * @param {Ext.data.Record} rec
68 createRecord : function(rec) {
69 return this.toHash(rec);
72 * Implements abstract Ext.data.DataWriter#updateRecord
74 * @param {Ext.data.Record} rec
77 updateRecord : function(rec) {
78 return this.toHash(rec);
82 * Implements abstract Ext.data.DataWriter#destroyRecord
84 * @param {Ext.data.Record} rec
87 destroyRecord : function(rec){
88 if(this.encodeDelete){
90 data[this.meta.idProperty] = rec.id;