PDA

View Full Version : Store same Model via a different response format



Bunchofstring
6 Dec 2012, 2:22 PM
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

{response:
{id:1, name:'Charles'}
}

...but favorites/2 returns

{data:{
person:{id:2, name:'Snoopy'},
favorites:[
{id:1, name:'Charles'},
{id:3, name:'Woodstock'}
]
}}

...and my Person model has a convert function like

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?

mitchellsimoens
8 Dec 2012, 6:11 AM
Looks like you have different roots, you need to have the reader configured to use the root that you need.

Bunchofstring
10 Dec 2012, 8:21 AM
@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)?

var store = Ext.getStore('Person'),
proxy = store.getProxy();

proxy.setUrl('favorites/2');
proxy.getReader().setRootProperty('data.favorites');
store.load();