PDA

View Full Version : json readrecords - missing ; before statement



ttbgwt
11 Sep 2012, 8:57 AM
I have a json store and the json returned to me includes a period in the data which looks like is causing the readRecords call to return the following error message:

"missing ; before statement"

I had an override that worked in 3.4 but don't know how to override this during our conversion to 4.1.1 ?



this.readRecords({
"metaData": {
"fields": [
{
"name": "date"
},
{
"convert": "value",
"name": ".LIBGO12M Index", //THIS IS CAUSING THE ISSUE
"permission_code": 1
}
],
"root": "rows"
},
"rows": [
],
"results": 22
});




Ext.define('Ext.data.reader.Json', {
extend: 'Ext.data.reader.Reader',
alternateClassName: 'Ext.data.JsonReader',
alias : 'reader.json',


root: '',


/**
* @cfg {String} record The optional location within the JSON response that the record data itself can be found at.
* See the JsonReader intro docs for more details. This is not often needed.
*/


/**
* @cfg {Boolean} useSimpleAccessors True to ensure that field names/mappings are treated as literals when
* reading values.
*
* For example, by default, using the mapping "foo.bar.baz" will try and read a property foo from the root, then a property bar
* from foo, then a property baz from bar. Setting the simple accessors to true will read the property with the name
* "foo.bar.baz" direct from the root object.
*/
useSimpleAccessors: false,


/**
* Reads a JSON object and returns a ResultSet. Uses the internal getTotal and getSuccess extractors to
* retrieve meta data from the response, and extractData to turn the JSON data into model instances.
* @param {Object} data The raw JSON data
* @return {Ext.data.ResultSet} A ResultSet containing model instances and meta data about the results
*/
readRecords: function(data) {
//this has to be before the call to super because we use the meta data in the superclass readRecords
if (data.metaData) {
this.onMetaChange(data.metaData);
}


/**
* @property {Object} jsonData
* A copy of this.rawData.
* @deprecated Will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead.
*/
this.jsonData = data;
return this.callParent([data]);
},


//inherit docs
getResponseData: function(response) {
var data, error;

try {
data = Ext.decode(response.responseText);
return this.readRecords(data);
} catch (ex) {
error = new Ext.data.ResultSet({
total : 0,
count : 0,
records: [],
success: false,
message: ex.message
});


this.fireEvent('exception', this, response, error);


Ext.Logger.warn('Unable to parse the JSON returned by the server');


return error;
}
},


//inherit docs
buildExtractors : function() {
var me = this;


me.callParent(arguments);


if (me.root) {
me.getRoot = me.createAccessor(me.root);
} else {
me.getRoot = function(root) {
return root;
};
}
},


/**
* @private
* We're just preparing the data for the superclass by pulling out the record objects we want. If a {@link #record}
* was specified we have to pull those out of the larger JSON object, which is most of what this function is doing
* @param {Object} root The JSON root node
* @return {Ext.data.Model[]} The records
*/
extractData: function(root) {
var recordName = this.record,
data = [],
length, i;


if (recordName) {
length = root.length;

if (!length && Ext.isObject(root)) {
length = 1;
root = [root];
}


for (i = 0; i < length; i++) {
data[i] = root[i][recordName];
}
} else {
data = root;
}
return this.callParent([data]);
},


/**
* @private
* @method
* Returns an accessor function for the given property string. Gives support for properties such as the following:
*
* - 'someProperty'
* - 'some.property'
* - 'some["property"]'
*
* This is used by buildExtractors to create optimized extractor functions when casting raw data into model instances.
*/
createAccessor: (function() {
var re = /[\[\.]/;


return function(expr) {
if (Ext.isEmpty(expr)) {
return Ext.emptyFn;
}
if (Ext.isFunction(expr)) {
return expr;
}
if (this.useSimpleAccessors !== true) {
var i = String(expr).search(re);
if (i >= 0) {
return Ext.functionFactory('obj', 'return obj' + (i > 0 ? '.' : '') + expr);
}
}
return function(obj) {
return obj[expr];
};
};
}()),


/**
* @private
* @method
* Returns an accessor expression for the passed Field. Gives support for properties such as the following:
*
* - 'someProperty'
* - 'some.property'
* - 'some["property"]'
*
* This is used by buildExtractors to create optimized on extractor function which converts raw data into model instances.
*/
createFieldAccessExpression: (function() {
var re = /[\[\.]/;


return function(field, fieldVarName, dataName) {
var me = this,
hasMap = (field.mapping !== null),
map = hasMap ? field.mapping : field.name,
result,
operatorSearch;


if (typeof map === 'function') {
result = fieldVarName + '.mapping(' + dataName + ', this)';
} else if (this.useSimpleAccessors === true || ((operatorSearch = String(map).search(re)) < 0)) {
if (!hasMap || isNaN(map)) {
// If we don't provide a mapping, we may have a field name that is numeric
map = '"' + map + '"';
}
result = dataName + "[" + map + "]";
} else {
result = dataName + (operatorSearch > 0 ? '.' : '') + map;
}
return result;
};
}())
});

mitchellsimoens
17 Sep 2012, 8:46 AM
Is it the '.' that is causing it?

ttbgwt
17 Sep 2012, 8:54 AM
Yes it is. I added useSimpleAccessors: true to my proxy's reader config and that solved my problem.