PDA

View Full Version : Does Sencha Touch Model always have to contain an Array?



Martin1982
30 Aug 2011, 6:05 AM
It seems like in this topic http://www.sencha.com/forum/showthread.php?125544-Does-EXTJS-4.0-Model-always-have-to-contain-an-Array/page2 order has been restored to the universe, but what about Sencha Touch?

I'm having the same issue now;
An external API, which is not returning a single record as an array of the object, but just as an object, and I want to read it.
Anyone know a good workaround for this?

fx-mike
30 Aug 2011, 8:49 AM
Write a custom reader and put the object inside of an array for further processing.
You can find a code sample here: http://www.sencha.com/forum/showthread.php?142759-RestProxy-—-need-a-different-root-for-different-actions-is-it-possible&p=633893&viewfull=1#post633893

Martin1982
30 Aug 2011, 12:25 PM
Thanks for the link, I got it working as a new reader;


Ext.data.JsonRecordReader = Ext.extend(Ext.data.JsonReader, {
/**
* 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) {
if (Ext.isArray(data)) {
data = data[0];
}

if (!data.results) {
data = {
'total': 1,
'skip': 0,
'limit': 20,
'results': [data]
}
}

return Ext.data.JsonRecordReader.superclass.readRecords.call(this, data);
}
});

Ext.data.ReaderMgr.registerType('jsonrecord', Ext.data.JsonRecordReader);