2 * @class Ext.data.writer.Json
3 * @extends Ext.data.writer.Writer
5 This class is used to write {@link Ext.data.Model} data to the server in a JSON format.
6 The {@link #allowSingle} configuration can be set to false to force the records to always be
7 encoded in an array, even if there is only a single record being sent.
11 Ext.define('Ext.data.writer.Json', {
12 extend: 'Ext.data.writer.Writer',
13 alternateClassName: 'Ext.data.JsonWriter',
17 * @cfg {String} root The key under which the records in this Writer will be placed. Defaults to <tt>undefined</tt>.
18 * Example generated request, using root: 'records':
20 {'records': [{name: 'my record'}, {name: 'another record'}]}
26 * @cfg {Boolean} encode True to use Ext.encode() on the data before sending. Defaults to <tt>false</tt>.
27 * The encode option should only be set to true when a {@link #root} is defined, because the values will be
28 * sent as part of the request parameters as opposed to a raw post. The root will be the name of the parameter
34 * @cfg {Boolean} allowSingle False to ensure that records are always wrapped in an array, even if there is only
35 * one record being sent. When there is more than one record, they will always be encoded into an array.
36 * Defaults to <tt>true</tt>. Example:
38 // with allowSingle: true
44 // with allowSingle: false
54 writeRecords: function(request, data) {
57 if (this.allowSingle && data.length == 1) {
58 // convert to single object format
64 // sending as a param, need to encode
65 request.params[root] = Ext.encode(data);
68 Ext.Error.raise('Must specify a root when using encode');
73 request.jsonData = request.jsonData || {};
75 request.jsonData[root] = data;
77 request.jsonData = data;