1. #1
    Ext User
    Join Date
    Jan 2010
    Location
    Poland, Warsaw
    Posts
    3
    Vote Rating
    0
    gadus is on a distinguished road

      0  

    Post Problem with mapping in JsonStore

    Problem with mapping in JsonStore


    Hi everyone,
    I have the following problem. I'm trying to consume ajax response in the folowing JSON format:
    Code:
    {
        "message": "Something good happened on the server!", 
        "data": [
            {
                "created_at": "2009-12-18 17:04:12", 
                "abstract": "Some abstract", 
                "state": {
                    "id": 5, 
                    "name": "Published"
                }, 
                "title": "Some title"
            }]
    }
    Javascript code responsible for consuming this response looks like this:
    Code:
    var store = new Ext.data.JsonStore({
    	proxy: new Ext.data.HttpProxy({
                 url: 'some_url',
    	     method: 'GET'
           }),
           root: 'data',
           fields: [
                 'title',
    	     'abstract',
    	     {name:'created_at', type:'date'},
    	     {name: 'state', mapping: 'state.name' }
           ]
    });
    When I do not make mapping for "state" field everything is OK, but with above mapping loading store loads no records without any error.

  2. #2
    Ext User
    Join Date
    Aug 2009
    Posts
    588
    Vote Rating
    1
    Mike Robinson is on a distinguished road

      0  

    Default


    If you look in JsonReader at the method "getJsonAccessor," you see there the code that is used to obtain the value. You can see that the function (closure...) it returns contains a try..catch statement which will silently absorb any errors.

    This is the place where you need to try to point your debugger... looks like it won't be easy.

  3. #3
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    83
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    You probably need:
    Code:
    {name: 'state', mapping: 'state', convert: function(v){return v ? v.name : null;}}
    (this won't fail if no state is present)

  4. #4
    Ext User
    Join Date
    Jan 2010
    Location
    Poland, Warsaw
    Posts
    3
    Vote Rating
    0
    gadus is on a distinguished road

      0  

    Default


    It works!!! Many thanks Condor. I've tested both options ('mapping' and 'convert') but separately, that's why it didn't work.

  5. #5
    Sencha User nak1's Avatar
    Join Date
    Jan 2008
    Posts
    266
    Vote Rating
    0
    nak1 is an unknown quantity at this point

      0  

    Default


    So what if we're mapping to an array within an object: {
    name: 'address_address',
    mapping: 'address.address[0].street',
    convert: function(v){
    return v ? v.address.address[0].street : null;

    }
    }

    I still get this error even when I do have a convert function defined. Is there a fix for this?
    No longer a Newbie

  6. #6
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    83
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    That should be:
    Code:
    {
      name: 'address_address',
      mapping: 'address',
      convert: function(v){
        return v && v.address && v.address[0] ? v.address[0].street : null;
      }
    }
    (are you sure it's address.address?)

  7. #7
    Sencha User
    Join Date
    Aug 2011
    Location
    India
    Posts
    11
    Vote Rating
    0
    nuzahat sayyed is on a distinguished road

      0  

    Default Unable to use mapping in sencha touch

    Unable to use mapping in sencha touch


    Hi Guys.
    I am facing a problem in sencha while mapping Ext.data.JsonStore model using Ext.data.JsonReader.


    Json response from server (server model):
    {"rows":[{"id":1,"firstname":"Bill"},{"id": 2,"firstname":"Ben"}]}


    Model used in Json Store:
    Ext.regModel( 'mycabinet', {
    fields: [
    { name : 'DeviceId', type: 'int' },
    'CabinetName']
    });




    json Reader code:
    var iccDeviceReader = new Ext.data.JsonReader({
    // metadata configuration options:
    idProperty: 'id',
    root: 'rows',
    fields: [
    {name: 'CabinetName', mapping: 'firstname'},
    {name:'DeviceId',mapping:'id'}
    ]
    });


    json store code:
    app.iccDS = new Ext.data.JsonStore( {
    model : 'mycabinet',
    sorters : 'CabinetName',
    getGroupString : function(record) { return record.get('CabinetName')[0]; },
    proxy : {
    type: 'ajax',
    url : '/icc/js/data.js',
    reader:iccDeviceReader
    },
    autoLoad: true
    } );


    I am expecting that "mycabinet" model will get populated with "server model". However, mapping doesnt occur.
    I even tried using convert without any success(name:'DeviceId',mapping:'id',convert: function(v){return v.id;})


    Any help will be highly appreciated.
    Thanks

  8. #8
    Sencha User carol.ext's Avatar
    Join Date
    Jun 2007
    Location
    Littleton, Colorado, USA
    Posts
    672
    Vote Rating
    20
    carol.ext will become famous soon enough carol.ext will become famous soon enough

      0  

    Default


    There is a forum for sencha touch issues.