Results 1 to 3 of 3

Thread: POSTing deeply nested XML/JSON data

  1. #1
    Sencha User
    Join Date
    Feb 2013
    Location
    Mumbai, INDIA
    Posts
    6
    Vote Rating
    0
      0  

    Default Answered: POSTing deeply nested XML/JSON data

    Hi all,

    I have a client side data object which looks like this one created after processing a form with some logic:

    Code:
    requestObj =
                    {
                            firstName: "John",
                            lastName : "Doe",
                addresses : [
                                {
                        address: {
                            houseNo : "42", 
                                            street : "abc",
                                            area: "def", 
                                            city: "ghi",
                                            country: "jkl"
                        }
                }    
                ]    
                    };

    I, then, package this object into a model instance created from this model definition:

    Code:
    Ext.define('personalInfo', {
        extend : 'Ext.data.Model',
        config : {
            fields : [
                {name : 'firstName'} ,
                {name : 'lastName'} ,
            ],
            hasMany : [
                {model : 'address', name : 'adresses', associationKey : 'addresses'},
            ]
        }
    });
    
    
    Ext.define('address', {
        extend : 'Ext.data.Model',
        config : {
            fields : [
                    'houseNo', 
                    'street',
                    'area',
                    'city',
                    'country',
            ]
        }
    });
    
    requestModelInstance = new personalInfo(requestObj);
    After this I am binding this with a store which has a proxy and writer configured to make a POST request to my server.



    Code:
    
    var personalInfoUpdateRequest = new Ext.data.Store({
        model : 'personalInfo',
            proxy : {
                    type : 'ajax',
                    url : 'serviceURL',
            headers: { 'Content-Type': 'application/json' },
                    writer : {
                        type : 'json',
                        encode : true,
                        rootProperty : 'personalInfo',
                    }
            }
    });
    
    
    personalInfoUpdateRequest.sync(); // To make the POST

    The POST request is going but the fields I see are only the top level ones, i.e., firstName and lastName. There is no trace of the 'addresses' array. I have tried both XML and JSON infact there seems to be some issue with the requestObj getting packaged into the requestModelInstance.

    There is, practically, no example anywhere in the docs which helps in doing something like this.

    Please help!
    Last edited by alok0412; 11 Apr 2013 at 5:22 AM. Reason: minor typo : requestObject at one place instead of requestObj.

  2. Syncing associated data is not yet supported. You could iterate through the records and associations to build your data structure to send.

  3. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3932
    Vote Rating
    1272
      0  

    Default

    Syncing associated data is not yet supported. You could iterate through the records and associations to build your data structure to send.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  4. #3
    Sencha User
    Join Date
    Feb 2013
    Location
    Mumbai, INDIA
    Posts
    6
    Vote Rating
    0
      0  

    Default

    Thanks for updating me on this, Mitchell!

    I had, unfortunately, wasted quite a lot of time trying to get this to work by switching my service and the client between JSON and XML and trying various tweaks.

    Finally, what I have stuck onto is sending out hand-made JSON objects after encoding as the payload for AJAX request.

    Code:
    requestObj =
            {
                firstName: "John",
                lastName : "Doe",
                addresses : [
                        {
                        address: {
                            houseNo : "42", 
                            street : "abc",
                            area: "def", 
                            city: "ghi",
                            country: "jkl"
                        }
                }    
                ]    
    };
    
    
    requestObject = Ext.JSON.encode(requestObj);
    
    
    Ext.Ajax.request({
                    url: myurl,
                    method: 'POST',
                    jsonData : requestObject,
                    headers: { 'Content-Type': 'application/json', 'Accept' : 'text/json' },
                    success: function(response, opts) {
                      var obj = response.responseText;
                      console.dir(obj);
                    },
                    failure: function(response, opts) {
                       console.log('server-side failure with status code ' + response.status);
                } 
    });

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •