Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / docs / source / JsonWriter.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.2.2
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.data.JsonWriter"></div>/**
16  * @class Ext.data.JsonWriter
17  * @extends Ext.data.DataWriter
18  * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action.
19  */
20 Ext.data.JsonWriter = Ext.extend(Ext.data.DataWriter, {
21     <div id="cfg-Ext.data.JsonWriter-encode"></div>/**
22      * @cfg {Boolean} encode <p><tt>true</tt> to {@link Ext.util.JSON#encode JSON encode} the
23      * {@link Ext.data.DataWriter#toHash hashed data} into a standard HTTP parameter named after this
24      * Reader's <code>meta.root</code> property which, by default is imported from the associated Reader. Defaults to <tt>true</tt>.</p>
25      * <p>If set to <code>false</code>, the hashed data is {@link Ext.util.JSON#encode JSON encoded}, along with
26      * the associated {@link Ext.data.Store}'s {@link Ext.data.Store#baseParams baseParams}, into the POST body.</p>
27      * <p>When using {@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>.</p>
31      * <p>When using a {@link Ext.data.Store#restful} Store, some serverside frameworks are
32      * tuned to expect data through the jsonData mechanism.  In those cases, one will want to set <b>encode: <tt>false</tt></b>, as in
33      * let the lower-level connection object (eg: Ext.Ajax) do the encoding.</p>
34      */
35     encode : true,
36     <div id="cfg-Ext.data.JsonWriter-encodeDelete"></div>/**
37      * @cfg {Boolean} encodeDelete False to send only the id to the server on delete, true to encode it in an object
38      * literal, eg: <pre><code>
39 {id: 1}
40  * </code></pre> Defaults to <tt>false</tt>
41      */
42     encodeDelete: false,
43     
44     constructor : function(config){
45         Ext.data.JsonWriter.superclass.constructor.call(this, config);    
46     },
47
48     <div id="method-Ext.data.JsonWriter-render"></div>/**
49      * <p>This method should not need to be called by application code, however it may be useful on occasion to
50      * override it, or augment it with an {@link Function#createInterceptor interceptor} or {@link Function#createSequence sequence}.</p>
51      * <p>The provided implementation encodes the serialized data representing the Store's modified Records into the Ajax request's
52      * <code>params</code> according to the <code>{@link #encode}</code> setting.</p>
53      * @param {Object} Ajax request params object to write into.
54      * @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}.
55      * @param {Object/Object[]} data Data object representing the serialized modified records from the Store. May be either a single object,
56      * or an Array of objects - user implementations must handle both cases.
57      */
58     render : function(params, baseParams, data) {
59         if (this.encode === true) {
60             // Encode here now.
61             Ext.apply(params, baseParams);
62             params[this.meta.root] = Ext.encode(data);
63         } else {
64             // defer encoding for some other layer, probably in {@link Ext.Ajax#request}.  Place everything into "jsonData" key.
65             var jdata = Ext.apply({}, baseParams);
66             jdata[this.meta.root] = data;
67             params.jsonData = jdata;
68         }
69     },
70     <div id="method-Ext.data.JsonWriter-createRecord"></div>/**
71      * Implements abstract Ext.data.DataWriter#createRecord
72      * @protected
73      * @param {Ext.data.Record} rec
74      * @return {Object}
75      */
76     createRecord : function(rec) {
77        return this.toHash(rec);
78     },
79     <div id="method-Ext.data.JsonWriter-updateRecord"></div>/**
80      * Implements abstract Ext.data.DataWriter#updateRecord
81      * @protected
82      * @param {Ext.data.Record} rec
83      * @return {Object}
84      */
85     updateRecord : function(rec) {
86         return this.toHash(rec);
87
88     },
89     <div id="method-Ext.data.JsonWriter-destroyRecord"></div>/**
90      * Implements abstract Ext.data.DataWriter#destroyRecord
91      * @protected
92      * @param {Ext.data.Record} rec
93      * @return {Object}
94      */
95     destroyRecord : function(rec){
96         if(this.encodeDelete){
97             var data = {};
98             data[this.meta.idProperty] = rec.id;
99             return data;
100         }else{
101             return rec.id;
102         }
103     }
104 });</pre>    
105 </body>
106 </html>