2
votes

I created custom json reader as suggest by James Clark in following post.

Here is a code for creating custom json

  Ext.define('MyReader', {
            extend: 'Ext.data.reader.Json',
            alias: 'my-json',
            read: function (object) {
                debugger;
                object.Results = Ext.decode(object.responseText);
                this.callParent([object]);
            }
        });

In store definition I assing custom reader

var store = Ext.create('Ext.data.Store', {
//model: 'Option',
    fields: fields,
    pageSize: itemsPerPage,
    proxy: {
        type: 'ajax',
        url: getDataWithPageURL,
        **reader:Ext.create('MyReader', {root: 'Results', totalProperty: 'Total'})**
    }
});

The json that I receiving from clients look like this

{"Results":["{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}","{\"BaseCurrency\":\"USD\",\"TermCurrency\":\"JPY\"}"],"Total":4}

I receiving an error in ext-js.js I debuged it and the error occurs in Ext.data.proxy.Server class in following code

if (success === true) {
        reader = me.getReader();
        result = reader.read(me.extractResponseData(response));
        records = result.records;

result is undefined.

Please help

1
See the edits to my answer in the original question. There were some bugs in my initial code.James Clark

1 Answers

4
votes

The problems with the code in my initial attempt were:

  1. the read() method should return a value, so it should say:
return this.callParent([object]);
  1. The alias should have been 'reader.my-json'

  2. The results needed to be mapped because it was an array:

object.Results = Ext.Array.map(object.Results, Ext.decode);

With those fixed, the store can use the simpler reader definition:

reader: {
  type: 'my-json',
  root: 'Results',
  totalProperty: 'Total'
}

But see the complete test case in the original question for the full code. I apologize for not having thoroughly tested the code I initially proposed.