2 * @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',
12 * @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':
15 {'records': [{name: 'my record'}, {name: 'another record'}]}
21 * @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
29 * @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:
33 // with allowSingle: true
39 // with allowSingle: false
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;