1. #1
    Sencha User
    Join Date
    Aug 2010
    Posts
    5
    Vote Rating
    0
    clements is on a distinguished road

      0  

    Default 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:

    Code:
    {
        "data":
        {
            "FirstName":"Bob",
            "LastName":"Vance"
        }
    }
    The object's data is wrapped in the root attribute. However, I would prefer the following instead:

    Code:
    {
        "FirstName":"Bob",
        "LastName":"Vance"
    }
    Is this possible and if so how?

  2. #2
    Sencha User
    Join Date
    Aug 2010
    Posts
    5
    Vote Rating
    0
    clements is on a distinguished road

      0  

    Default


    I found the guilty party. Here's the code in ext-all.js that wraps the object in the root tag:

    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;
            }
        },
    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?

  3. #3
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Answers
    13
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Inject your own render method which does exactly what you want?

  4. #4
    Sencha User
    Join Date
    Aug 2010
    Posts
    5
    Vote Rating
    0
    clements is on a distinguished road

      0  

    Default


    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.

  5. #5
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Answers
    13
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    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.

  6. #6
    Sencha User
    Join Date
    Jul 2010
    Posts
    11
    Vote Rating
    0
    gmarziou is on a distinguished road

      0  

    Default


    Quote Originally Posted by Animal View Post
    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?

    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

  7. #7
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Answers
    13
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    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

  8. #8
    Sencha User
    Join Date
    Jul 2010
    Posts
    11
    Vote Rating
    0
    gmarziou is on a distinguished road

      0  

    Default


    Thanks.

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

Similar Threads

  1. JSON with more than one root
    By D-L in forum Ext 2.x: Help & Discussion
    Replies: 8
    Last Post: 11 Aug 2012, 1:25 AM
  2. JsonStore saving is wrapping JSON data in the root property
    By Ranma13 in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 27 May 2010, 6:41 PM
  3. Tracing JSON without specifying the root
    By D-L in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 8 Jan 2009, 1:48 AM
  4. Json without root
    By Ilikefish in forum Ext 2.x: Help & Discussion
    Replies: 14
    Last Post: 13 Aug 2008, 11:39 AM
  5. JSON root
    By choni in forum Ext 1.x: Help & Discussion
    Replies: 1
    Last Post: 19 Jan 2007, 8:15 AM

Thread Participants: 2

Tags for this Thread