* @extends Roo.data.DataReader
* Data reader class to create an Array of Roo.data.Record objects from a JSON response
* based on mappings in a provided Roo.data.Record constructor.
+ *
+ * The default behaviour of a store is to send ?_requestMeta=1, unless the class has recieved 'metaData' property
+ * in the reply previously.
+ *
* <p>
* Example code:
* <pre><code>
* @cfg {String} successProperty Name of the property from which to retrieve the success attribute used by forms.
* @cfg {String} root name of the property which contains the Array of row objects.
* @cfg {String} id Name of the property within a row object that contains a record identifier value.
+ * @cfg {Array} fields Array of field definition objects
* @constructor
* Create a new JsonReader
* @param {Object} meta Metadata configuration options
Roo.data.JsonReader.superclass.constructor.call(this, meta, recordType||meta.fields);
};
Roo.extend(Roo.data.JsonReader, Roo.data.DataReader, {
+
+ readerType : 'Json',
+
+ /**
+ * @prop {Boolean} metaFromRemote - if the meta data was loaded from the remote source.
+ * Used by Store query builder to append _requestMeta to params.
+ *
+ */
+ metaFromRemote : false,
/**
* This method is only used by a DataProxy which has retrieved data from a remote server.
* @param {Object} response The XHR object which contains the JSON data in its responseText.
*/
read : function(response){
var json = response.responseText;
- /* eval:var:o */
- var o = eval("("+json+")");
+
+ var o = /* eval:var:o */ eval("("+json+")");
if(!o) {
throw {message: "JsonReader.read: Json object not found"};
}
if(o.metaData){
+
delete this.ef;
+ this.metaFromRemote = true;
this.meta = o.metaData;
this.recordType = Roo.data.Record.create(o.metaData.fields);
this.onMetaChange(this.meta, this.recordType, o);
* After any data loads, the raw JSON data is available for further custom processing.
* @type Object
*/
- this.jsonData = o;
+ this.o = o;
var s = this.meta, Record = this.recordType,
- f = Record.prototype.fields, fi = f.items, fl = f.length;
+ f = Record ? Record.prototype.fields : null, fi = f ? f.items : [], fl = f ? f.length : 0;
// Generate extraction functions for the totalProperty, the root, the id, and for each field
if (!this.ef) {
if (s.id) {
var g = this.getJsonAccessor(s.id);
this.getId = function(rec) {
- var r = g(rec);
+ var r = g(rec);
return (r === undefined || r === "") ? null : r;
};
} else {
this.getId = function(){return null;};
}
this.ef = [];
- for(var i = 0; i < fl; i++){
- f = fi[i];
+ for(var jj = 0; jj < fl; jj++){
+ f = fi[jj];
var map = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
- this.ef[i] = this.getJsonAccessor(map);
+ this.ef[jj] = this.getJsonAccessor(map);
}
}
var root = this.getRoot(o), c = root.length, totalRecords = c, success = true;
if(s.totalProperty){
- var v = parseInt(this.getTotal(o), 10);
- if(!isNaN(v)){
- totalRecords = v;
+ var vt = parseInt(this.getTotal(o), 10);
+ if(!isNaN(vt)){
+ totalRecords = vt;
}
}
if(s.successProperty){
- var v = this.getSuccess(o);
- if(v === false || v === 'false'){
+ var vs = this.getSuccess(o);
+ if(vs === false || vs === 'false'){
success = false;
}
}
var records = [];
- for(var i = 0; i < c; i++){
- var n = root[i];
- var values = {};
- var id = this.getId(n);
- for(var j = 0; j < fl; j++){
- f = fi[j];
- var v = this.ef[j](n);
- values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue);
- }
- var record = new Record(values, id);
- record.json = n;
- records[i] = record;
- }
- return {
- success : success,
- records : records,
- totalRecords : totalRecords
- };
+ for(var i = 0; i < c; i++){
+ var n = root[i];
+ var values = {};
+ var id = this.getId(n);
+ for(var j = 0; j < fl; j++){
+ f = fi[j];
+ var v = this.ef[j](n);
+ if (!f.convert) {
+ Roo.log('missing convert for ' + f.name);
+ Roo.log(f);
+ continue;
+ }
+ values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue);
+ }
+ var record = new Record(values, id);
+ record.json = n;
+ records[i] = record;
+ }
+ return {
+ raw : o,
+ success : success,
+ records : records,
+ totalRecords : totalRecords
+ };
+ },
+ // used when loading children.. @see loadDataFromChildren
+ toLoadData: function(rec)
+ {
+ // expect rec just to be an array.. eg [a,b,c, [...] << cn ]
+ var data = typeof(rec.data.cn) == 'undefined' ? [] : rec.data.cn;
+ return { data : data, total : data.length };
+
}
});
\ No newline at end of file