4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.data.JsonReader
14 * @extends Roo.data.DataReader
15 * Data reader class to create an Array of Roo.data.Record objects from a JSON response
16 * based on mappings in a provided Roo.data.Record constructor.
20 var RecordDef = Roo.data.Record.create([
21 {name: 'name', mapping: 'name'}, // "mapping" property not needed if it's the same as "name"
22 {name: 'occupation'} // This field will use "occupation" as the mapping.
24 var myReader = new Roo.data.JsonReader({
25 totalProperty: "results", // The property which contains the total dataset size (optional)
26 root: "rows", // The property which contains an Array of row objects
27 id: "id" // The property within each row object that provides an ID for the record (optional)
31 * This would consume a JSON file like this:
33 { 'results': 2, 'rows': [
34 { 'id': 1, 'name': 'Bill', occupation: 'Gardener' },
35 { 'id': 2, 'name': 'Ben', occupation: 'Horticulturalist' } ]
38 * @cfg {String} totalProperty Name of the property from which to retrieve the total number of records
39 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
40 * paged from the remote server.
41 * @cfg {String} successProperty Name of the property from which to retrieve the success attribute used by forms.
42 * @cfg {String} root name of the property which contains the Array of row objects.
43 * @cfg {String} id Name of the property within a row object that contains a record identifier value.
45 * Create a new JsonReader
46 * @param {Object} meta Metadata configuration options
47 * @param {Object} recordType Either an Array of field definition objects,
48 * or an {@link Roo.data.Record} object created using {@link Roo.data.Record#create}.
50 Roo.data.JsonReader = function(meta, recordType){
55 totalProperty: 'total',
56 successProperty : 'success',
61 Roo.data.JsonReader.superclass.constructor.call(this, meta, recordType||meta.fields);
63 Roo.extend(Roo.data.JsonReader, Roo.data.DataReader, {
65 * This method is only used by a DataProxy which has retrieved data from a remote server.
66 * @param {Object} response The XHR object which contains the JSON data in its responseText.
67 * @return {Object} data A data block which is used by an Roo.data.Store object as
68 * a cache of Roo.data.Records.
70 read : function(response){
71 var json = response.responseText;
73 var o = eval("("+json+")");
75 throw {message: "JsonReader.read: Json object not found"};
80 this.meta = o.metaData;
81 this.recordType = Roo.data.Record.create(o.metaData.fields);
82 this.onMetaChange(this.meta, this.recordType, o);
84 return this.readRecords(o);
87 // private function a store will implement
88 onMetaChange : function(meta, recordType, o){
95 simpleAccess: function(obj, subsc) {
102 getJsonAccessor: function(){
104 return function(expr) {
106 return(re.test(expr))
107 ? new Function("obj", "return obj." + expr)
117 * Create a data block containing Roo.data.Records from an XML document.
118 * @param {Object} o An object which contains an Array of row objects in the property specified
119 * in the config as 'root, and optionally a property, specified in the config as 'totalProperty'
120 * which contains the total size of the dataset.
121 * @return {Object} data A data block which is used by an Roo.data.Store object as
122 * a cache of Roo.data.Records.
124 readRecords : function(o){
126 * After any data loads, the raw JSON data is available for further custom processing.
130 var s = this.meta, Record = this.recordType,
131 f = Record.prototype.fields, fi = f.items, fl = f.length;
133 // Generate extraction functions for the totalProperty, the root, the id, and for each field
135 if(s.totalProperty) {
136 this.getTotal = this.getJsonAccessor(s.totalProperty);
138 if(s.successProperty) {
139 this.getSuccess = this.getJsonAccessor(s.successProperty);
141 this.getRoot = s.root ? this.getJsonAccessor(s.root) : function(p){return p;};
143 var g = this.getJsonAccessor(s.id);
144 this.getId = function(rec) {
146 return (r === undefined || r === "") ? null : r;
149 this.getId = function(){return null;};
152 for(var i = 0; i < fl; i++){
154 var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
155 this.ef[i] = this.getJsonAccessor(map);
159 var root = this.getRoot(o), c = root.length, totalRecords = c, success = true;
161 var v = parseInt(this.getTotal(o), 10);
166 if(s.successProperty){
167 var v = this.getSuccess(o);
168 if(v === false || v === 'false'){
173 for(var i = 0; i < c; i++){
176 var id = this.getId(n);
177 for(var j = 0; j < fl; j++){
179 var v = this.ef[j](n);
180 values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue);
182 var record = new Record(values, id);
189 totalRecords : totalRecords