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.reader.Json'>/**
2 </span> * @author Ed Spencer
3 * @class Ext.data.reader.Json
4 * @extends Ext.data.reader.Reader
6 * <p>The JSON Reader is used by a Proxy to read a server response that is sent back in JSON format. This usually
7 * happens as a result of loading a Store - for example we might create something like this:</p>
9 <pre><code>
11 extend: 'Ext.data.Model',
12 fields: ['id', 'name', 'email']
15 var store = new Ext.data.Store({
25 </code></pre>
27 * <p>The example above creates a 'User' model. Models are explained in the {@link Ext.data.Model Model} docs if you're
28 * not already familiar with them.</p>
30 * <p>We created the simplest type of JSON Reader possible by simply telling our {@link Ext.data.Store Store}'s
31 * {@link Ext.data.proxy.Proxy Proxy} that we want a JSON Reader. The Store automatically passes the configured model to the
32 * Store, so it is as if we passed this instead:
34 <pre><code>
39 </code></pre>
41 * <p>The reader we set up is ready to read data from our server - at the moment it will accept a response like this:</p>
43 <pre><code>
47 "name": "Ed Spencer",
48 "email": "ed@sencha.com"
52 "name": "Abe Elias",
53 "email": "abe@sencha.com"
56 </code></pre>
58 * <p><u>Reading other JSON formats</u></p>
60 * <p>If you already have your JSON format defined and it doesn't look quite like what we have above, you can usually
61 * pass JsonReader a couple of configuration options to make it parse your format. For example, we can use the
62 * {@link #root} configuration to parse data that comes back like this:</p>
64 <pre><code>
69 "name": "Ed Spencer",
70 "email": "ed@sencha.com"
74 "name": "Abe Elias",
75 "email": "abe@sencha.com"
79 </code></pre>
81 * <p>To parse this we just pass in a {@link #root} configuration that matches the 'users' above:</p>
83 <pre><code>
88 </code></pre>
90 * <p>Sometimes the JSON structure is even more complicated. Document databases like CouchDB often provide metadata
91 * around each record inside a nested structure like this:</p>
93 <pre><code>
95 "total": 122,
96 "offset": 0,
99 "id": "ed-spencer-1",
100 "value": 1,
103 "name": "Ed Spencer",
104 "email": "ed@sencha.com"
109 </code></pre>
111 * <p>In the case above the record data is nested an additional level inside the "users" array as each "user" item has
112 * additional metadata surrounding it ('id' and 'value' in this case). To parse data out of each "user" item in the
113 * JSON above we need to specify the {@link #record} configuration like this:</p>
115 <pre><code>
121 </code></pre>
123 * <p><u>Response metadata</u></p>
125 * <p>The server can return additional data in its response, such as the {@link #totalProperty total number of records}
126 * and the {@link #successProperty success status of the response}. These are typically included in the JSON response
127 * like this:</p>
129 <pre><code>
131 "total": 100,
132 "success": true,
136 "name": "Ed Spencer",
137 "email": "ed@sencha.com"
141 </code></pre>
143 * <p>If these properties are present in the JSON response they can be parsed out by the JsonReader and used by the
144 * Store that loaded it. We can set up the names of these properties by specifying a final pair of configuration
147 <pre><code>
151 totalProperty : 'total',
152 successProperty: 'success'
154 </code></pre>
156 * <p>These final options are not necessary to make the Reader work, but can be useful when the server needs to report
157 * an error or if it needs to indicate that there is a lot of data available of which only a subset is currently being
158 * returned.</p>
160 Ext.define('Ext.data.reader.Json', {
161 extend: 'Ext.data.reader.Reader',
162 alternateClassName: 'Ext.data.JsonReader',
163 alias : 'reader.json',
167 <span id='Ext-data.reader.Json-cfg-record'> /**
168 </span> * @cfg {String} record The optional location within the JSON response that the record data itself can be found at.
169 * See the JsonReader intro docs for more details. This is not often needed and defaults to undefined.
172 <span id='Ext-data.reader.Json-cfg-useSimpleAccessors'> /**
173 </span> * @cfg {Boolean} useSimpleAccessors True to ensure that field names/mappings are treated as literals when
174 * reading values. Defalts to <tt>false</tt>.
175 * For example, by default, using the mapping "foo.bar.baz" will try and read a property foo from the root, then a property bar
176 * from foo, then a property baz from bar. Setting the simple accessors to true will read the property with the name
177 * "foo.bar.baz" direct from the root object.
179 useSimpleAccessors: false,
181 <span id='Ext-data.reader.Json-method-readRecords'> /**
182 </span> * Reads a JSON object and returns a ResultSet. Uses the internal getTotal and getSuccess extractors to
183 * retrieve meta data from the response, and extractData to turn the JSON data into model instances.
184 * @param {Object} data The raw JSON data
185 * @return {Ext.data.ResultSet} A ResultSet containing model instances and meta data about the results
187 readRecords: function(data) {
188 //this has to be before the call to super because we use the meta data in the superclass readRecords
190 this.onMetaChange(data.metaData);
193 <span id='Ext-data.reader.Json-property-jsonData'> /**
194 </span> * DEPRECATED - will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead
198 this.jsonData = data;
199 return this.callParent([data]);
203 getResponseData: function(response) {
205 var data = Ext.decode(response.responseText);
210 json: response.responseText,
212 msg: 'Unable to parse the JSON returned by the server: ' + ex.toString()
217 Ext.Error.raise('JSON object not found');
225 buildExtractors : function() {
228 me.callParent(arguments);
231 me.getRoot = me.createAccessor(me.root);
233 me.getRoot = function(root) {
239 <span id='Ext-data.reader.Json-method-extractData'> /**
241 * We're just preparing the data for the superclass by pulling out the record objects we want. If a {@link #record}
242 * was specified we have to pull those out of the larger JSON object, which is most of what this function is doing
243 * @param {Object} root The JSON root node
244 * @return {Array} The records
246 extractData: function(root) {
247 var recordName = this.record,
252 length = root.length;
254 for (i = 0; i < length; i++) {
255 data[i] = root[i][recordName];
260 return this.callParent([data]);
263 <span id='Ext-data.reader.Json-method-createAccessor'> /**
265 * Returns an accessor function for the given property string. Gives support for properties such as the following:
268 * 'some["property"]'
269 * This is used by buildExtractors to create optimized extractor functions when casting raw data into model instances.
271 createAccessor: function() {
274 return function(expr) {
275 if (Ext.isEmpty(expr)) {
278 if (Ext.isFunction(expr)) {
281 if (this.useSimpleAccessors !== true) {
282 var i = String(expr).search(re);
284 return Ext.functionFactory('obj', 'return obj' + (i > 0 ? '.' : '') + expr);
287 return function(obj) {
292 });</pre></pre></body></html>