PDA

View Full Version : How to use Ext.data.writer.Json to create a model object with nameProperty applied



nohsib
3 Nov 2012, 8:02 AM
I have nameProperty set in the proxy of my store like this :


writer : {
type : 'json',
nameProperty : 'mapping',
root : 'PerVO'
}


And in my model , the mapping like this:




{
name : 'modules',
mapping : 'modules.collection',
defaultValue: []
},


This works fine when I call the CRUD operations on the store. Now I want to get the data in the format being sent to the server for other operations. The problem is that when I get the record from the store , the mapping is lost. So how can I useExt.data.writer.Json or some other api to generate the data EXACTLY as it would be generated while being sent to the server with mapping applied :

This is what the store sends for save (I need the data like this. Note : Difference is 'collection:')


"modules": {
"collection": {
"isActive": 1,
"metadata": {
"actionCodes": "",
"memos": ""
},
"moduleCode": "OM",
"moduleId": 250,
"moduleName": "Org Management",
"success": false,
"viewId": 0
}
},



This is what i get from the store when the record is looked up:


"modules": {
"isActive": 1,
"metadata": {
"actionCodes": "",
"memos": ""
},
"moduleCode": "OM",
"moduleId": 250,
"moduleName": "Org Management",
"success": false,
"viewId": 0
},


I need to get the data as sent by the store to server.
Thanks
Nohsib

vietits
3 Nov 2012, 4:51 PM
You should define a serialize() method for "modules" field. Try to fix your code as below:


writer : {
type : 'json',
// nameProperty : 'mapping', <- no need
root : 'PerVO'
}




...
{
name : 'modules',
mapping : 'modules.collection',
defaultValue: [],
serialize: function(v){
return {
collection: v
};
}
},