-
16 Aug 2010 6:22 AM #1
Unanswered: Not wrapping JSON with root?
Unanswered: Not wrapping JSON with root?
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:
The object's data is wrapped in the root attribute. However, I would prefer the following instead:Code:{ "data": { "FirstName":"Bob", "LastName":"Vance" } }
Is this possible and if so how?Code:{ "FirstName":"Bob", "LastName":"Vance" }
-
16 Aug 2010 3:57 PM #2
I found the guilty party. Here's the code in ext-all.js that wraps the object in the root tag:
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?Code: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; } },
-
16 Aug 2010 11:24 PM #3Sencha - Ext JS Dev Team
- Join Date
- Mar 2007
- Location
- Notts/Redwood City
- Posts
- 30,458
- Vote Rating
- 20
- Answers
- 9
Inject your own render method which does exactly what you want?
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
17 Aug 2010 5:28 AM #4
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.
-
17 Aug 2010 5:45 AM #5Sencha - Ext JS Dev Team
- Join Date
- Mar 2007
- Location
- Notts/Redwood City
- Posts
- 30,458
- Vote Rating
- 20
- Answers
- 9
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.
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
17 Aug 2010 6:07 AM #6
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?
Code: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
-
17 Aug 2010 6:26 AM #7Sencha - Ext JS Dev Team
- Join Date
- Mar 2007
- Location
- Notts/Redwood City
- Posts
- 30,458
- Vote Rating
- 20
- Answers
- 9
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
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
18 Aug 2010 1:43 AM #8
Thanks.
As an alternative, I am looking at adapting Spring MVC on server side to cope with this root.
Similar Threads
-
JSON with more than one root
By D-L in forum Ext 2.x: Help & DiscussionReplies: 8Last Post: 11 Aug 2012, 1:25 AM -
JsonStore saving is wrapping JSON data in the root property
By Ranma13 in forum Ext 3.x: Help & DiscussionReplies: 3Last Post: 27 May 2010, 6:41 PM -
Tracing JSON without specifying the root
By D-L in forum Ext 2.x: Help & DiscussionReplies: 2Last Post: 8 Jan 2009, 1:48 AM -
Json without root
By Ilikefish in forum Ext 2.x: Help & DiscussionReplies: 14Last Post: 13 Aug 2008, 11:39 AM -
JSON root
By choni in forum Ext 1.x: Help & DiscussionReplies: 1Last Post: 19 Jan 2007, 8:15 AM


Reply With Quote