1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.writer.Json'>/**
2 </span> * @class Ext.data.writer.Json
3 * @extends Ext.data.writer.Writer
6 Ext.define('Ext.data.writer.Json', {
7 extend: 'Ext.data.writer.Writer',
8 alternateClassName: 'Ext.data.JsonWriter',
11 <span id='Ext-data.writer.Json-cfg-root'> /**
12 </span> * @cfg {String} root The key under which the records in this Writer will be placed. Defaults to <tt>undefined</tt>.
13 * Example generated request, using root: 'records':
14 <pre><code>
15 {'records': [{name: 'my record'}, {name: 'another record'}]}
16 </code></pre>
20 <span id='Ext-data.writer.Json-cfg-encode'> /**
21 </span> * @cfg {Boolean} encode True to use Ext.encode() on the data before sending. Defaults to <tt>false</tt>.
22 * The encode option should only be set to true when a {@link #root} is defined, because the values will be
23 * sent as part of the request parameters as opposed to a raw post. The root will be the name of the parameter
28 <span id='Ext-data.writer.Json-cfg-allowSingle'> /**
29 </span> * @cfg {Boolean} allowSingle False to ensure that records are always wrapped in an array, even if there is only
30 * one record being sent. When there is more than one record, they will always be encoded into an array.
31 * Defaults to <tt>true</tt>. Example:
32 * <pre><code>
33 // with allowSingle: true
35 "first": "Mark",
36 "last": "Corrigan"
39 // with allowSingle: false
41 "first": "Mark",
42 "last": "Corrigan"
44 * </code></pre>
49 writeRecords: function(request, data) {
52 if (this.allowSingle && data.length == 1) {
53 // convert to single object format
59 // sending as a param, need to encode
60 request.params[root] = Ext.encode(data);
63 Ext.Error.raise('Must specify a root when using encode');
68 request.jsonData = request.jsonData || {};
70 request.jsonData[root] = data;
72 request.jsonData = data;
78 </pre></pre></body></html>