Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / src / data / JsonWriter.js
1 /*!
2  * Ext JS Library 3.1.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
15     // careful to respect "returnJson", renamed to "encode"
16     // TODO: remove after Ext-3.0.1 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, will be removed in Ext-3.0.1</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 {Object} http params-object to write-to.
41      * @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}.
42      * @param {Object/Object[]} data Data-object representing compiled Store-recordset.
43      */
44     render : function(params, baseParams, data) {
45         if (this.encode === true) {
46             // Encode here now.
47             Ext.apply(params, baseParams);
48             params[this.meta.root] = Ext.encode(data);
49         } else {
50             // defer encoding for some other layer, probably in {@link Ext.Ajax#request}.  Place everything into "jsonData" key.
51             var jdata = Ext.apply({}, baseParams);
52             jdata[this.meta.root] = data;
53             params.jsonData = jdata;
54         }
55     },
56     /**
57      * Implements abstract Ext.data.DataWriter#createRecord
58      * @protected
59      * @param {Ext.data.Record} rec
60      * @return {Object}
61      */
62     createRecord : function(rec) {
63        return this.toHash(rec);
64     },
65     /**
66      * Implements abstract Ext.data.DataWriter#updateRecord
67      * @protected
68      * @param {Ext.data.Record} rec
69      * @return {Object}
70      */
71     updateRecord : function(rec) {
72         return this.toHash(rec);
73
74     },
75     /**
76      * Implements abstract Ext.data.DataWriter#destroyRecord
77      * @protected
78      * @param {Ext.data.Record} rec
79      * @return {Object}
80      */
81     destroyRecord : function(rec) {
82         return rec.id;
83     }
84 });