Skip to content

Instantly share code, notes, and snippets.

@frow
Created May 21, 2012 16:14
Show Gist options
  • Save frow/2763080 to your computer and use it in GitHub Desktop.
Save frow/2763080 to your computer and use it in GitHub Desktop.
/**
* A reader which is used to read polymorphic associations.
* Implementors have to provide a getPolyModel function.
*/
Ext.define('Your.PolymorphicReader', {
extend : 'Ext.data.reader.Json',
extractData : function(root) {
var me = this, values = [], records = [], tempModel, i = 0, length = root.length, node, id, record;
if(!root.length && Ext.isObject(root)) {
root = [root];
length = 1;
}
for(; i < length; i++) {
node = root[i];
tempModel = me.getPolyModel(node);
if(tempModel) {
me.setModel(tempModel);
} else {
continue;
}
values = me.extractValues(node);
id = me.getId(node);
record = new tempModel(values, id, node);
records.push(record);
if(me.implicitIncludes) {
me.readAssociated(record, node);
}
}
return records;
},
root : 'data',
totalProperty : 'total',
successProperty : 'success'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment