4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-writer-Json'>/**
19 </span> * @class Ext.data.writer.Json
20 * @extends Ext.data.writer.Writer
22 This class is used to write {@link Ext.data.Model} data to the server in a JSON format.
23 The {@link #allowSingle} configuration can be set to false to force the records to always be
24 encoded in an array, even if there is only a single record being sent.
28 Ext.define('Ext.data.writer.Json', {
29 extend: 'Ext.data.writer.Writer',
30 alternateClassName: 'Ext.data.JsonWriter',
33 <span id='Ext-data-writer-Json-cfg-root'> /**
34 </span> * @cfg {String} root The key under which the records in this Writer will be placed. Defaults to <tt>undefined</tt>.
35 * Example generated request, using root: 'records':
36 <pre><code>
37 {'records': [{name: 'my record'}, {name: 'another record'}]}
38 </code></pre>
42 <span id='Ext-data-writer-Json-cfg-encode'> /**
43 </span> * @cfg {Boolean} encode True to use Ext.encode() on the data before sending. Defaults to <tt>false</tt>.
44 * The encode option should only be set to true when a {@link #root} is defined, because the values will be
45 * sent as part of the request parameters as opposed to a raw post. The root will be the name of the parameter
50 <span id='Ext-data-writer-Json-cfg-allowSingle'> /**
51 </span> * @cfg {Boolean} allowSingle False to ensure that records are always wrapped in an array, even if there is only
52 * one record being sent. When there is more than one record, they will always be encoded into an array.
53 * Defaults to <tt>true</tt>. Example:
54 * <pre><code>
55 // with allowSingle: true
57 "first": "Mark",
58 "last": "Corrigan"
61 // with allowSingle: false
63 "first": "Mark",
64 "last": "Corrigan"
66 * </code></pre>
71 writeRecords: function(request, data) {
74 if (this.allowSingle && data.length == 1) {
75 // convert to single object format
81 // sending as a param, need to encode
82 request.params[root] = Ext.encode(data);
85 Ext.Error.raise('Must specify a root when using encode');
90 request.jsonData = request.jsonData || {};
92 request.jsonData[root] = data;
94 request.jsonData = data;