1. #1
    Sencha User
    Join Date
    Feb 2013
    Location
    Mumbai, INDIA
    Posts
    6
    Vote Rating
    0
    alok0412 is on a distinguished road

      0  

    Default Answered: POSTing deeply nested XML/JSON data

    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 - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,346
    Answers
    3541
    Vote Rating
    847
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      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 @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Feb 2013
    Location
    Mumbai, INDIA
    Posts
    6
    Vote Rating
    0
    alok0412 is on a distinguished road

      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);
                } 
    });