Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / src / data / JsonWriter.js
1 /*!
2  * Ext JS Library 3.0.0
3  * Copyright(c) 2006-2009 Ext JS, LLC
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 = function(config) {
13     Ext.data.JsonWriter.superclass.constructor.call(this, config);
14     // careful to respect "returnJson", renamed to "encode"
15     if (this.returnJson != undefined) {
16         this.encode = this.returnJson;
17     }
18 }
19 Ext.extend(Ext.data.JsonWriter, Ext.data.DataWriter, {
20     /**
21      * @cfg {Boolean} returnJson <b>Deprecated.  Use {@link Ext.data.JsonWriter#encode} instead.
22      */
23     returnJson : undefined,
24     /**
25      * @cfg {Boolean} encode <tt>true</tt> to {@link Ext.util.JSON#encode encode} the
26      * {@link Ext.data.DataWriter#toHash hashed data}. Defaults to <tt>true</tt>.  When using
27      * {@link Ext.data.DirectProxy}, set this to <tt>false</tt> since Ext.Direct.JsonProvider will perform
28      * its own json-encoding.  In addition, if you're using {@link Ext.data.HttpProxy}, setting to <tt>false</tt>
29      * will cause HttpProxy to transmit data using the <b>jsonData</b> configuration-params of {@link Ext.Ajax#request}
30      * instead of <b>params</b>.  When using a {@link Ext.data.Store#restful} Store, some serverside frameworks are
31      * tuned to expect data through the jsonData mechanism.  In those cases, one will want to set <b>encode: <tt>false</tt></b>
32      */
33     encode : true,
34
35     /**
36      * Final action of a write event.  Apply the written data-object to params.
37      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
38      * @param {Record[]} rs
39      * @param {Object} http params
40      * @param {Object} data object populated according to DataReader meta-data "root" and "idProperty"
41      */
42     render : function(action, rs, params, data) {
43         Ext.apply(params, data);
44
45         if (this.encode === true) { // <-- @deprecated returnJson
46             if (Ext.isArray(rs) && data[this.meta.idProperty]) {
47                 params[this.meta.idProperty] = Ext.encode(params[this.meta.idProperty]);
48             }
49             params[this.meta.root] = Ext.encode(params[this.meta.root]);
50         }
51     },
52     /**
53      * createRecord
54      * @protected
55      * @param {Ext.data.Record} rec
56      */
57     createRecord : function(rec) {
58         return this.toHash(rec);
59     },
60     /**
61      * updateRecord
62      * @protected
63      * @param {Ext.data.Record} rec
64      */
65     updateRecord : function(rec) {
66         return this.toHash(rec);
67
68     },
69     /**
70      * destroyRecord
71      * @protected
72      * @param {Ext.data.Record} rec
73      */
74     destroyRecord : function(rec) {
75         return rec.id;
76     }
77 });