PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/ext-4.1.0_b3/src/data/writer/Json.js

https://bitbucket.org/srogerf/javascript
JavaScript | 81 lines | 29 code | 6 blank | 46 comment | 10 complexity | b8759d9f0c8bd71f4367e048cbc9556e MD5 | raw file
 1/**
 2 * @class Ext.data.writer.Json
 3
 4This class is used to write {@link Ext.data.Model} data to the server in a JSON format.
 5The {@link #allowSingle} configuration can be set to false to force the records to always be
 6encoded in an array, even if there is only a single record being sent.
 7
 8 * @markdown
 9 */
10Ext.define('Ext.data.writer.Json', {
11    extend: 'Ext.data.writer.Writer',
12    alternateClassName: 'Ext.data.JsonWriter',
13    alias: 'writer.json',
14    
15    /**
16     * @cfg {String} root The key under which the records in this Writer will be placed. Defaults to <tt>undefined</tt>.
17     * Example generated request, using root: 'records':
18<pre><code>
19{'records': [{name: 'my record'}, {name: 'another record'}]}
20</code></pre>
21     */
22    root: undefined,
23    
24    /**
25     * @cfg {Boolean} encode True to use Ext.encode() on the data before sending. Defaults to <tt>false</tt>.
26     * The encode option should only be set to true when a {@link #root} is defined, because the values will be
27     * sent as part of the request parameters as opposed to a raw post. The root will be the name of the parameter
28     * sent to the server.
29     */
30    encode: false,
31    
32    /**
33     * @cfg {Boolean} allowSingle False to ensure that records are always wrapped in an array, even if there is only
34     * one record being sent. When there is more than one record, they will always be encoded into an array.
35     * Defaults to <tt>true</tt>. Example:
36     * <pre><code>
37// with allowSingle: true
38"root": {
39    "first": "Mark",
40    "last": "Corrigan"
41}
42
43// with allowSingle: false
44"root": [{
45    "first": "Mark",
46    "last": "Corrigan"
47}]
48     * </code></pre>
49     */
50    allowSingle: true,
51    
52    //inherit docs
53    writeRecords: function(request, data) {
54        var root = this.root;
55        
56        if (this.allowSingle && data.length == 1) {
57            // convert to single object format
58            data = data[0];
59        }
60        
61        if (this.encode) {
62            if (root) {
63                // sending as a param, need to encode
64                request.params[root] = Ext.encode(data);
65            } else {
66                //<debug>
67                Ext.Error.raise('Must specify a root when using encode');
68                //</debug>
69            }
70        } else {
71            // send as jsonData
72            request.jsonData = request.jsonData || {};
73            if (root) {
74                request.jsonData[root] = data;
75            } else {
76                request.jsonData = data;
77            }
78        }
79        return request;
80    }
81});