Json3.html
3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-data-writer-Json'>/**
</span> * @class Ext.data.writer.Json
This class is used to write {@link Ext.data.Model} data to the server in a JSON format.
The {@link #allowSingle} configuration can be set to false to force the records to always be
encoded in an array, even if there is only a single record being sent.
* @markdown
*/
Ext.define('Ext.data.writer.Json', {
extend: 'Ext.data.writer.Writer',
alternateClassName: 'Ext.data.JsonWriter',
alias: 'writer.json',
<span id='Ext-data-writer-Json-cfg-root'> /**
</span> * @cfg {String} root The key under which the records in this Writer will be placed. Defaults to <tt>undefined</tt>.
* Example generated request, using root: 'records':
<pre><code>
{'records': [{name: 'my record'}, {name: 'another record'}]}
</code></pre>
*/
root: undefined,
<span id='Ext-data-writer-Json-cfg-encode'> /**
</span> * @cfg {Boolean} encode True to use Ext.encode() on the data before sending. Defaults to <tt>false</tt>.
* The encode option should only be set to true when a {@link #root} is defined, because the values will be
* sent as part of the request parameters as opposed to a raw post. The root will be the name of the parameter
* sent to the server.
*/
encode: false,
<span id='Ext-data-writer-Json-cfg-allowSingle'> /**
</span> * @cfg {Boolean} allowSingle False to ensure that records are always wrapped in an array, even if there is only
* one record being sent. When there is more than one record, they will always be encoded into an array.
* Defaults to <tt>true</tt>. Example:
* <pre><code>
// with allowSingle: true
"root": {
"first": "Mark",
"last": "Corrigan"
}
// with allowSingle: false
"root": [{
"first": "Mark",
"last": "Corrigan"
}]
* </code></pre>
*/
allowSingle: true,
//inherit docs
writeRecords: function(request, data) {
var root = this.root;
if (this.allowSingle && data.length == 1) {
// convert to single object format
data = data[0];
}
if (this.encode) {
if (root) {
// sending as a param, need to encode
request.params[root] = Ext.encode(data);
} else {
//<debug>
Ext.Error.raise('Must specify a root when using encode');
//</debug>
}
} else {
// send as jsonData
request.jsonData = request.jsonData || {};
if (root) {
request.jsonData[root] = data;
} else {
request.jsonData = data;
}
}
return request;
}
});
</pre>
</body>
</html>