Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / src / data / JsonWriter.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
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.
11  */
12 Ext.data.JsonWriter = Ext.extend(Ext.data.DataWriter, {
13     /**
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.
22      */
23     encode : true,
24     /**
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>
27 {id: 1}
28  * </code></pre> Defaults to <tt>false</tt>
29      */
30     encodeDelete: false,
31     
32     constructor : function(config){
33         Ext.data.JsonWriter.superclass.constructor.call(this, config);    
34     },
35
36     /**
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.
41      */
42     render : function(params, baseParams, data) {
43         if (this.encode === true) {
44             // Encode here now.
45             Ext.apply(params, baseParams);
46             params[this.meta.root] = Ext.encode(data);
47         } else {
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;
52         }
53     },
54     /**
55      * Implements abstract Ext.data.DataWriter#createRecord
56      * @protected
57      * @param {Ext.data.Record} rec
58      * @return {Object}
59      */
60     createRecord : function(rec) {
61        return this.toHash(rec);
62     },
63     /**
64      * Implements abstract Ext.data.DataWriter#updateRecord
65      * @protected
66      * @param {Ext.data.Record} rec
67      * @return {Object}
68      */
69     updateRecord : function(rec) {
70         return this.toHash(rec);
71
72     },
73     /**
74      * Implements abstract Ext.data.DataWriter#destroyRecord
75      * @protected
76      * @param {Ext.data.Record} rec
77      * @return {Object}
78      */
79     destroyRecord : function(rec){
80         if(this.encodeDelete){
81             var data = {};
82             data[this.meta.idProperty] = rec.id;
83             return data;
84         }else{
85             return rec.id;
86         }
87     }
88 });