PDA

View Full Version : Not wrapping JSON with root?



clements
16 Aug 2010, 6:22 AM
I have an EditorGridPanel configured with a JsonStore. The JsonStore has a root attribute set to "data". After updating a row in the grid, I noticed that the following is sent to the server:



{
"data":
{
"FirstName":"Bob",
"LastName":"Vance"
}
}


The object's data is wrapped in the root attribute. However, I would prefer the following instead:



{
"FirstName":"Bob",
"LastName":"Vance"
}


Is this possible and if so how?

clements
16 Aug 2010, 3:57 PM
I found the guilty party. Here's the code in ext-all.js that wraps the object in the root tag:


Ext.data.JsonWriter = Ext.extend(Ext.data.DataWriter, {
...,
render : function(params, baseParams, data) {
if (this.encode === true) {
...
} else {
var jdata = Ext.apply({}, baseParams);
jdata[this.meta.root] = data;
params.jsonData = jdata;
}
},

So, now I'm wondering what event can I tap into to remove the data tag from the params.jsonData object. Should I add a listener for the DataProxy.beforewrite event?

Animal
16 Aug 2010, 11:24 PM
Inject your own render method which does exactly what you want?

clements
17 Aug 2010, 5:28 AM
Good idea. I have been shy about extending anything yet as I'm new to Ext JS, but I will give it a try.

What is the reasoning for wrapping the root node with the root? I have marked the grid as RESTful. Accordingly, the response includes something like PUT /Employee/Edit/5 where 5 is the ID of the employee to edit. In this scenario, we are dealing with a single object. Yet, ext-all.js wraps the root node? I might expect this if I had multiple employee objects in the response but not one. I ask this question because I wonder if I have misunderstood something architecturally.

Animal
17 Aug 2010, 5:45 AM
What if you were deleting (or updating) several records? You'd want "data" to exist then. It would have to reference an Array. That's why it's there. Because Store can batch operations. You can add, remove and delete records locally, and then synch with the server.

gmarziou
17 Aug 2010, 6:07 AM
What if you were deleting (or updating) several records? You'd want "data" to exist then. It would have to reference an Array. That's why it's there. Because Store can batch operations. You can add, remove and delete records locally, and then synch with the server.

I extended the JSonWriter for same reasons as Clement due to a SPring REST backend and in my understanding store does not batch operations when restful is true.

So am I correct to say that my change below is safe as long as my store is restful?



Ext.ns('MyApp.data');
MyApp.data.JsonWriter = Ext.extend(Ext.data.JsonWriter, {

/**
* Overrides parent's render to suppress root in JSON string.
*/
render : function(params, baseParams, data) {
if (this.encode === true) {
// Encode here now.
params = Ext.encode(data);
} else {
// defer encoding for some other layer, probably in {@link Ext.Ajax#request}.
params.jsonData = data;
}
}
});


Thanks

Animal
17 Aug 2010, 6:26 AM
Looking at the code it looks like it will try to send batches. But as long as you don't let it, that should be OK

gmarziou
18 Aug 2010, 1:43 AM
Thanks.

As an alternative, I am looking at adapting Spring MVC on server side to cope with this root.