Patton
Patton

Reputation: 2032

Writing a Custom Reader in ExtJS

I am new to ExtJS and am playing around with it, I am receiving following JSON from the server as shown below

{"tid":1,"action":"HelloWorld","method":"getData",
  "result": {"InstId":"1",
    "value":  [
  {"country.stateId":"101", "country.stateName":"TA"},
  {"country.stateId":"102", "country.stateName":"ABC"}, 
  {"country.stateId":"103", "country.stateName":"DEF"}]}",
                  "type":"rpc"}

and I need to extract values form the above JSON and populate the combobox. To do this job I need to write a custom Reader to read and populate the in the combobox. Can anyone of you please help me in writing a custom reader?

Following are the various snippet

Ext.define('AM.store.TestStore', {

  extend: 'Ext.data.Store',

  alias : 'widget.testStore',
  model: 'AM.model.TestModel',
  autoload:true,
  proxy: {
      type: 'direct',

      directFn: HelloWorld.getData,
      extraParams:[ {'InstId': '1', 'boxid':'id'},
                    {'InstId': '1', 'name':'states'}
   ],

   reader:{
         type:'json',
         root:'result'

  }
  });

Following is the view object

Ext.define('AM.view.combobox.TestView' ,{
    extend: 'Ext.form.ComboBox',
    alias : 'widget.TestView',
    fieldLabel: 'States',
    store:'TestStore',
    renderTo: Ext.getBody(),
    displayField: 'country.stateName',
    valueField: 'country.stateId',
    autoselect:false

});

Following is my Model Object

Ext.define('AM.model.TestModel', {
    extend: 'Ext.data.Model',

    alias : 'widget.TestModel',
    fields: [ {name: 'country.stateId'}, {name: 'country.stateName'}]
);

Following is my controller

Ext.define('AM.controller.Funds', {
     extend: 'Ext.app.Controller',
     alias : 'widget.FundsController',
     views: ['combobox.TestView'],
     stores: ['TestStore'],
     models:['TestModel']
);

Can anyone please help me to write a custom JSON Reader?

Thanks
Phani Kumar

Upvotes: 2

Views: 1882

Answers (1)

sha
sha

Reputation: 17860

You don't need to write custom reader for that. I think making couple small changes in your code would be enough:

First, in the proxy definition:

root: 'value'
useSimpleAccessors: true

then in the model for your data:

fields: [{
    name: 'id', mapping: 'country.stateId' }, {
    name: 'name', mapping: 'country.stateName' 
}]

That should do it.

Upvotes: 2

Related Questions