3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.data.JsonReader"></div>/**
9 * @class Ext.data.JsonReader
10 * @extends Ext.data.DataReader
11 * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from a JSON response
12 * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
13 * <p>Example code:</p>
15 var Employee = Ext.data.Record.create([
16 {name: 'firstname'}, // map the Record's "firstname" field to the row object's key of the same name
17 {name: 'job', mapping: 'occupation'} // map the Record's "job" field to the row object's "occupation" key
19 var myReader = new Ext.data.JsonReader(
20 { // The metadata property, with configuration options:
21 totalProperty: "results", // the property which contains the total dataset size (optional)
22 root: "rows", // the property which contains an Array of record data objects
23 idProperty: "id" // the property within each row object that provides an ID for the record (optional)
25 Employee // {@link Ext.data.Record} constructor that provides mapping for JSON object
28 * <p>This would consume a JSON data object of the form:</p><pre><code>
30 results: 2, // Reader's configured totalProperty
31 rows: [ // Reader's configured root
32 { id: 1, firstname: 'Bill', occupation: 'Gardener' }, // a row object
33 { id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' } // another row object
37 * <p><b><u>Automatic configuration using metaData</u></b></p>
38 * <p>It is possible to change a JsonReader's metadata at any time by including a <b><tt>metaData</tt></b>
39 * property in the JSON data object. If the JSON data object has a <b><tt>metaData</tt></b> property, a
40 * {@link Ext.data.Store Store} object using this Reader will reconfigure itself to use the newly provided
41 * field definition and fire its {@link Ext.data.Store#metachange metachange} event. The metachange event
42 * handler may interrogate the <b><tt>metaData</tt></b> property to perform any configuration required.
43 * Note that reconfiguring a Store potentially invalidates objects which may refer to Fields or Records
44 * which no longer exist.</p>
45 * <p>The <b><tt>metaData</tt></b> property in the JSON data object may contain:</p>
46 * <div class="mdetail-params"><ul>
47 * <li>any of the configuration options for this class</li>
48 * <li>a <b><tt>{@link Ext.data.Record#fields fields}</tt></b> property which the JsonReader will
49 * use as an argument to the {@link Ext.data.Record#create data Record create method} in order to
50 * configure the layout of the Records it will produce.</li>
51 * <li>a <b><tt>{@link Ext.data.Store#sortInfo sortInfo}</tt></b> property which the JsonReader will
52 * use to set the {@link Ext.data.Store}'s {@link Ext.data.Store#sortInfo sortInfo} property</li>
53 * <li>any user-defined properties needed</li>
55 * <p>To use this facility to send the same data as the example above (without having to code the creation
56 * of the Record constructor), you would create the JsonReader like this:</p><pre><code>
57 var myReader = new Ext.data.JsonReader();
59 * <p>The first data packet from the server would configure the reader by containing a
60 * <b><tt>metaData</tt></b> property <b>and</b> the data. For example, the JSON data object might take
67 totalProperty: 'results',
70 {name: 'job', mapping: 'occupation'}
72 sortInfo: {field: 'name', direction:'ASC'}, // used by store to set its sortInfo
73 foo: 'bar' // custom property
77 { 'id': 1, 'name': 'Bill', occupation: 'Gardener' },
78 { 'id': 2, 'name': 'Ben', occupation: 'Horticulturalist' }
82 * @cfg {String} totalProperty [total] Name of the property from which to retrieve the total number of records
83 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
84 * paged from the remote server. Defaults to <tt>total</tt>.
85 * @cfg {String} successProperty [success] Name of the property from which to
86 * retrieve the success attribute. Defaults to <tt>success</tt>. See
87 * {@link Ext.data.DataProxy}.{@link Ext.data.DataProxy#exception exception}
88 * for additional information.
89 * @cfg {String} root [undefined] <b>Required</b>. The name of the property
90 * which contains the Array of row objects. Defaults to <tt>undefined</tt>.
91 * An exception will be thrown if the root property is undefined. The data packet
92 * value for this property should be an empty array to clear the data or show
94 * @cfg {String} idProperty [id] Name of the property within a row object that contains a record identifier value. Defaults to <tt>id</tt>
96 * Create a new JsonReader
97 * @param {Object} meta Metadata configuration options.
98 * @param {Array/Object} recordType
99 * <p>Either an Array of {@link Ext.data.Field Field} definition objects (which
100 * will be passed to {@link Ext.data.Record#create}, or a {@link Ext.data.Record Record}
101 * constructor created from {@link Ext.data.Record#create}.</p>
103 Ext.data.JsonReader = function(meta, recordType){
106 // default idProperty, successProperty & totalProperty -> "id", "success", "total"
109 successProperty: 'success',
110 totalProperty: 'total'
113 Ext.data.JsonReader.superclass.constructor.call(this, meta, recordType || meta.fields);
115 Ext.extend(Ext.data.JsonReader, Ext.data.DataReader, {
117 * This JsonReader's metadata as passed to the constructor, or as passed in
118 * the last data packet's <b><tt>metaData</tt></b> property.
123 * This method is only used by a DataProxy which has retrieved data from a remote server.
124 * @param {Object} response The XHR object which contains the JSON data in its responseText.
125 * @return {Object} data A data block which is used by an Ext.data.Store object as
126 * a cache of Ext.data.Records.
128 read : function(response){
129 var json = response.responseText;
130 var o = Ext.decode(json);
132 throw {message: "JsonReader.read: Json object not found"};
134 return this.readRecords(o);
137 // private function a store will implement
138 onMetaChange : function(meta, recordType, o){
145 simpleAccess: function(obj, subsc) {
152 getJsonAccessor: function(){
154 return function(expr) {
156 return(re.test(expr)) ?
157 new Function("obj", "return obj." + expr) :
167 * Create a data block containing Ext.data.Records from a JSON object.
168 * @param {Object} o An object which contains an Array of row objects in the property specified
169 * in the config as 'root, and optionally a property, specified in the config as 'totalProperty'
170 * which contains the total size of the dataset.
171 * @return {Object} data A data block which is used by an Ext.data.Store object as
172 * a cache of Ext.data.Records.
174 readRecords : function(o){
176 * After any data loads, the raw JSON data is available for further custom processing. If no data is
177 * loaded or there is a load exception this property will be undefined.
183 this.meta = o.metaData;
184 this.recordType = Ext.data.Record.create(o.metaData.fields);
185 this.onMetaChange(this.meta, this.recordType, o);
187 var s = this.meta, Record = this.recordType,
188 f = Record.prototype.fields, fi = f.items, fl = f.length, v;
190 var root = this.getRoot(o), c = root.length, totalRecords = c, success = true;
192 v = parseInt(this.getTotal(o), 10);
197 if(s.successProperty){
198 v = this.getSuccess(o);
199 if(v === false || v === 'false'){
205 for(var i = 0; i < c; i++){
207 var record = new Record(this.extractValues(n, fi, fl), this.getId(n));
214 totalRecords : totalRecords
219 buildExtractors : function() {
223 var s = this.meta, Record = this.recordType,
224 f = Record.prototype.fields, fi = f.items, fl = f.length;
226 if(s.totalProperty) {
227 this.getTotal = this.getJsonAccessor(s.totalProperty);
229 if(s.successProperty) {
230 this.getSuccess = this.getJsonAccessor(s.successProperty);
232 this.getRoot = s.root ? this.getJsonAccessor(s.root) : function(p){return p;};
233 if (s.id || s.idProperty) {
234 var g = this.getJsonAccessor(s.id || s.idProperty);
235 this.getId = function(rec) {
237 return (r === undefined || r === "") ? null : r;
240 this.getId = function(){return null;};
243 for(var i = 0; i < fl; i++){
245 var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
246 ef.push(this.getJsonAccessor(map));
251 // private extractValues
252 extractValues: function(data, items, len) {
254 for(var j = 0; j < len; j++){
256 var v = this.ef[j](data);
257 values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
263 * Decode a json response from server.
264 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
265 * @param {Object} response
267 readResponse : function(action, response) {
268 var o = (response.responseText !== undefined) ? Ext.decode(response.responseText) : response;
270 throw new Ext.data.JsonReader.Error('response');
272 if (Ext.isEmpty(o[this.meta.successProperty])) {
273 throw new Ext.data.JsonReader.Error('successProperty-response', this.meta.successProperty);
275 // TODO, separate empty and undefined exceptions.
276 if (action === Ext.data.Api.actions.create) {
277 if (Ext.isEmpty(o[this.meta.root])) {
278 throw new Ext.data.JsonReader.Error('root-emtpy', this.meta.root);
280 else if (o[this.meta.root] === undefined) {
281 throw new Ext.data.JsonReader.Error('root-undefined-response', this.meta.root);
290 * @class Ext.data.JsonReader.Error
291 * Error class for JsonReader
293 Ext.data.JsonReader.Error = Ext.extend(Ext.Error, {
294 constructor : function(message, arg) {
296 Ext.Error.call(this, message);
298 name : 'Ext.data.JsonReader'
300 Ext.apply(Ext.data.JsonReader.Error.prototype, {
302 'response': "An error occurred while json-decoding your server response",
303 'successProperty-response': 'Could not locate your "successProperty" in your server response. Please review your JsonReader config to ensure the config-property "successProperty" matches the property in your server-response. See the JsonReader docs.',
304 'root-undefined-response': 'Could not locate your "root" property in your server response. Please review your JsonReader config to ensure the config-property "root" matches the property your server-response. See the JsonReader docs.',
305 'root-undefined-config': 'Your JsonReader was configured without a "root" property. Please review your JsonReader config and make sure to define the root property. See the JsonReader docs.',
306 'idProperty-undefined' : 'Your JsonReader was configured without an "idProperty" Please review your JsonReader configuration and ensure the "idProperty" is set (e.g.: "id"). See the JsonReader docs.',
307 'root-emtpy': 'Data was expected to be returned by the server in the "root" property of the response. Please review your JsonReader configuration to ensure the "root" property matches that returned in the server-response. See JsonReader docs.'