3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.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 <tt>true</tt> to {@link Ext.util.JSON#encode encode} the
15 * {@link Ext.data.DataWriter#toHash hashed data}. Defaults to <tt>true</tt>. When using
16 * {@link Ext.data.DirectProxy}, set this to <tt>false</tt> since Ext.Direct.JsonProvider will perform
17 * its own json-encoding. In addition, if you're using {@link Ext.data.HttpProxy}, setting to <tt>false</tt>
18 * will cause HttpProxy to transmit data using the <b>jsonData</b> configuration-params of {@link Ext.Ajax#request}
19 * instead of <b>params</b>. When using a {@link Ext.data.Store#restful} Store, some serverside frameworks are
20 * tuned to expect data through the jsonData mechanism. In those cases, one will want to set <b>encode: <tt>false</tt></b>, as in
21 * let the lower-level connection object (eg: Ext.Ajax) do the encoding.
25 * @cfg {Boolean} encodeDelete False to send only the id to the server on delete, true to encode it in an object
26 * literal, eg: <pre><code>
28 * </code></pre> Defaults to <tt>false</tt>
32 constructor : function(config){
33 Ext.data.JsonWriter.superclass.constructor.call(this, config);
37 * Final action of a write event. Apply the written data-object to params.
38 * @param {Object} http params-object to write-to.
39 * @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}.
40 * @param {Object/Object[]} data Data-object representing compiled Store-recordset.
42 render : function(params, baseParams, data) {
43 if (this.encode === true) {
45 Ext.apply(params, baseParams);
46 params[this.meta.root] = Ext.encode(data);
48 // defer encoding for some other layer, probably in {@link Ext.Ajax#request}. Place everything into "jsonData" key.
49 var jdata = Ext.apply({}, baseParams);
50 jdata[this.meta.root] = data;
51 params.jsonData = jdata;
55 * Implements abstract Ext.data.DataWriter#createRecord
57 * @param {Ext.data.Record} rec
60 createRecord : function(rec) {
61 return this.toHash(rec);
64 * Implements abstract Ext.data.DataWriter#updateRecord
66 * @param {Ext.data.Record} rec
69 updateRecord : function(rec) {
70 return this.toHash(rec);
74 * Implements abstract Ext.data.DataWriter#destroyRecord
76 * @param {Ext.data.Record} rec
79 destroyRecord : function(rec){
80 if(this.encodeDelete){
82 data[this.meta.idProperty] = rec.id;