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