Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / data / writer / Json.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.data.writer.Json
17  * @extends Ext.data.writer.Writer
18
19 This class is used to write {@link Ext.data.Model} data to the server in a JSON format.
20 The {@link #allowSingle} configuration can be set to false to force the records to always be
21 encoded in an array, even if there is only a single record being sent.
22
23  * @markdown
24  */
25 Ext.define('Ext.data.writer.Json', {
26     extend: 'Ext.data.writer.Writer',
27     alternateClassName: 'Ext.data.JsonWriter',
28     alias: 'writer.json',
29     
30     /**
31      * @cfg {String} root The key under which the records in this Writer will be placed. Defaults to <tt>undefined</tt>.
32      * Example generated request, using root: 'records':
33 <pre><code>
34 {'records': [{name: 'my record'}, {name: 'another record'}]}
35 </code></pre>
36      */
37     root: undefined,
38     
39     /**
40      * @cfg {Boolean} encode True to use Ext.encode() on the data before sending. Defaults to <tt>false</tt>.
41      * The encode option should only be set to true when a {@link #root} is defined, because the values will be
42      * sent as part of the request parameters as opposed to a raw post. The root will be the name of the parameter
43      * sent to the server.
44      */
45     encode: false,
46     
47     /**
48      * @cfg {Boolean} allowSingle False to ensure that records are always wrapped in an array, even if there is only
49      * one record being sent. When there is more than one record, they will always be encoded into an array.
50      * Defaults to <tt>true</tt>. Example:
51      * <pre><code>
52 // with allowSingle: true
53 "root": {
54     "first": "Mark",
55     "last": "Corrigan"
56 }
57
58 // with allowSingle: false
59 "root": [{
60     "first": "Mark",
61     "last": "Corrigan"
62 }]
63      * </code></pre>
64      */
65     allowSingle: true,
66     
67     //inherit docs
68     writeRecords: function(request, data) {
69         var root = this.root;
70         
71         if (this.allowSingle && data.length == 1) {
72             // convert to single object format
73             data = data[0];
74         }
75         
76         if (this.encode) {
77             if (root) {
78                 // sending as a param, need to encode
79                 request.params[root] = Ext.encode(data);
80             } else {
81                 //<debug>
82                 Ext.Error.raise('Must specify a root when using encode');
83                 //</debug>
84             }
85         } else {
86             // send as jsonData
87             request.jsonData = request.jsonData || {};
88             if (root) {
89                 request.jsonData[root] = data;
90             } else {
91                 request.jsonData = data;
92             }
93         }
94         return request;
95     }
96 });
97