Skip to content

Instantly share code, notes, and snippets.

@Thinkscape
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thinkscape/641f4a7a16e3baebd78a to your computer and use it in GitHub Desktop.
Save Thinkscape/641f4a7a16e3baebd78a to your computer and use it in GitHub Desktop.
Fix reading of entities containing "length" property.
/**
* Fix reading of entities containing "length" property
*
* @link http://www.sencha.com/forum/showthread.php?292395-data.Reader-throwing-exception-when-model-contains-quot-length-quot-field.&p=1067807
* @link https://fiddle.sencha.com/#fiddle/at6
*/
Ext.define('Ext.bugfix.Reader', {
override: 'Ext.data.reader.Reader',
extractData: function(root, readOptions) {
var me = this,
entityType = readOptions && readOptions.model ? Ext.data.schema.Schema.lookupEntity(readOptions.model) : me.getModel(),
schema = entityType.schema,
includes = schema.hasAssociations(entityType) && me.getImplicitIncludes(),
fieldExtractorInfo = me.getFieldExtractorInfo(entityType.fieldExtractors),
length = root.length,
records = new Array(length),
typeProperty = me.getTypeProperty(),
reader, node, nodeType, record, i;
if (Ext.isObject(root)) {
root = [root];
length = 1;
}
for (i = 0; i < length; i++) {
record = root[i];
if (!record.isModel) {
// If we're given a model instance in the data, just push it on
// without doing any conversion. Otherwise, create a record.
node = record;
// This Reader may be configured to produce different model types based on
// a differentiator field in the incoming data:
// typeProperty name be a string, a function which yields the child type, or an object: {
// name: 'mtype',
// namespace: 'MyApp'
// }
if (typeProperty && (nodeType = me.getChildType(schema, node, typeProperty))) {
reader = nodeType.getProxy().getReader();
record = reader.extractRecord(node, readOptions, nodeType,
schema.hasAssociations(nodeType) && reader.getImplicitIncludes(),
reader.getFieldExtractorInfo(nodeType.fieldExtractors));
} else {
record = me.extractRecord(node, readOptions, entityType, includes,
fieldExtractorInfo);
}
if (record.isModel) {
// Trees need to be able to access the raw data (the XML node) in order to process its children.
record.raw = node;
}
}
if (record.onLoad) {
record.onLoad();
}
records[i] = record;
}
return records;
},
deprecate: {
'5.0.2' : {
methods: {
extractData: function() {
//<debug>
Ext.log.warn("This bugfix is probably deprecated!");
//</debug>
return Ext.data.reader.Reader.prototype.extractData.apply(this, arguments);
}
}
}
}
});