1. #1
    Sencha User
    Join Date
    Nov 2010
    Posts
    118
    Vote Rating
    9
    Bunchofstring will become famous soon enough

      0  

    Default Store same Model via a different response format

    Store same Model via a different response format


    What's the best way to get server data into a store if the response format is different from one web service to another? Example:

    user/1 returns
    Code:
    {response:
        {id:1, name:'Charles'}
    }
    ...but favorites/2 returns
    Code:
    {data:{
        person:{id:2, name:'Snoopy'},
        favorites:[
            {id:1, name:'Charles'},
            {id:3, name:'Woodstock'}
        ]
    }}
    ...and my Person model has a convert function like
    Code:
    Ext.define('Person',{
        extend:'Ext.data.Model',
        config:{
            fields:[
                {
                   name:'name', 
                   type:'string',
                   convert:function(value,record){
                       //If the name is Charles, change it to Blockhead
                       if(value == 'Charles'){return 'Blockhead';}
                       else{return value;}
                   }
               }
            ]
        }
    })
    In other words, how can I ensure that Charles gets stored properly regardless of the source web service?

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,047
    Vote Rating
    850
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Looks like you have different roots, you need to have the reader configured to use the root that you need.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Nov 2010
    Posts
    118
    Vote Rating
    9
    Bunchofstring will become famous soon enough

      0  

    Default


    @Mitchell, I need a way to accommodate both services with the same Model and Store. Are you suggesting that I do something like this (shown below)?
    Code:
    var store = Ext.getStore('Person'),
        proxy = store.getProxy();
    
    proxy.setUrl('favorites/2');
    proxy.getReader().setRootProperty('data.favorites');
    store.load();

Thread Participants: 1