PDA

View Full Version : Structuring store data back into the same format reader received them.



sergiob
18 Nov 2011, 8:33 AM
I have a simple json with inner object:


{
id:"aaa"
address:{
line1:"Elms str 1",
zip:"000"
}
}

I've spend some time making a Reader which moves address to top level, for example:


{
id:"aaa"
address.line1:"Elms str 1",
address.zip:"000"
}

After the code was done, I accidentally found out that ExtJs anyways interpret the first JSON as the second, so my code was in vain, but the problem arise when I try to save (by using this.getStore().sync() ) it tries to save the second json fields like "address.line1" it doesn't transform them back into internal structure like address: {line1: ""}}

Is there a way to tell ExtJs to transform point separated JSON fields into internal structures, or this time I really should write a custom Writer?

mitchellsimoens
18 Nov 2011, 2:39 PM
Can I see your entire store? That is weird and shouldn't create JSON like that.

sergiob
18 Nov 2011, 10:01 PM
Sorry, I'm not at my work computer, so I can't write the exact store, but it looks something like this:


Ext.define('MyApp.model.User',{
extend:'Ext.data.Model',
fields:[
{name:'userID', mapping:'id'},
'address.line1',
'address.zip']
});
Ext.define('MyApp.store.MyStore',{
extend:'Ext.data.Store',
model: 'MyApp.model.User'
proxy:{
type:'ajax',
url:'data/users.json',
reader:{
type:'json',
root:'users'
}
writer: {
//something about mapping I don't remember exactly but it's one line to map fields back
//for example 'userID' to 'id'
}
}
});

By the way, what JSON it shouldn't create? The resulting with fields like 'address.line1' ?

mitchellsimoens
19 Nov 2011, 5:14 PM
By the way, what JSON it shouldn't create? The resulting with fields like 'address.line1' ?

Right... it should be like "address : {"line1": "...."}

sergiob
20 Nov 2011, 1:58 AM
I thought the same, it's why I'm asking, may be there is a config option somewhere, because for example for mapping it reads everything ok, but to write you should configure something like mapping field so that Writer will write to the same fields it reads.