1. #1
    Sencha User
    Join Date
    Apr 2008
    Location
    West Linton, Scotland
    Posts
    244
    Vote Rating
    0
    andycramb is on a distinguished road

      0  

    Default Inserting new nested data in to an existing Record

    Inserting new nested data in to an existing Record


    I have json data in the format below
    I display the top level memebers in a grid
    The grid is bound to a form wher I display the top level memebers
    Inside there is a nested comments object
    I display the comments data inside this object to the right of the form using an Xtemplate to run over the items. I have a version online at http://www.cramb.org.uk/extjs/

    What I want to do is to allow the users to edit the form
    I have this for the top level memebers and it displays back on to the grid afetr saving the data

    What I also want to do is to add a new comment in the comment area of the textarea on the form
    This would mean updating the same record and appending nested data to the comments object.
    This would then update the store and then I want to refresh the comments to the right of the form to reflect the new data
    See the appended data in red below
    I need a steer on how I can achieve this as at the moment I cannot figure out how I can do this or where to start
    Any help is much appreciated
    Andy

    Code:
        "success": true,
        rows:[{
             "id": "1",
             "dueDate": "01/03/2009",
             "creationDate": "10/12/2008",
             "period": "Q4 2008",
             "type": "Sales",
             "description": "Support the Vision tendering process",
             "customer": "Callum Lindsay",
             "performer": "Andy Cramb",
             "community": "Vision",
             "state": "Acknowledgment",
             "lastAct": "Assert Complete",
             "scope": "private",
             "openClose": "open",
             "comments": [{
                     "commentId": "1",
                     "person": "Andy Cramb",
                     "act": "New Promise",
                     "date": "16/12/2008 09:21",
                     "comment": "I aim to support the Vision tendering process"
                     },{
                     "commentId": "2",
                     "person": "Andy Cramb",
                     "act": "Comment",
                     "date": "17/12/2008 10:21",
                     "comment": "I aim to adopt several approaches"
                     },{
                     "commentId": "3",
                     "person": "Callum Lindsay",
                     "act": "Comment",
                     "date": "18/12/2008 11:21",
                     "comment": "I can support you here Andy"
                     },{ //potential new record after the textarea in the form has new text and the form is saved
                     "commentId": "4",
                     "person": "Callum Lindsay",
                     "act": "Comment",
                     "date": "18/12/2008 11:21",
                     "comment": "I can support you here Andy"
            }]
        }]
    };
    Code:
    //existing record
    var myData = {
        "success": true,
        rows:[{
             "id": "1",
             "dueDate": "01/03/2009",
             "creationDate": "10/12/2008",
             "period": "Q4 2008",
             "type": "Sales",
             "description": "Support the Vision tendering process",
             "customer": "Callum Lindsay",
             "performer": "Andy Cramb",
             "community": "Vision",
             "state": "Acknowledgment",
             "lastAct": "Assert Complete",
             "scope": "private",
             "openClose": "open",
             "comments": [{
                     "commentId": "1",
                     "person": "Andy Cramb",
                     "act": "New Promise",
                     "date": "16/12/2008 09:21",
                     "comment": "I aim to support the Vision tendering process"
                     },{
                     "commentId": "2",
                     "person": "Andy Cramb",
                     "act": "Comment",
                     "date": "17/12/2008 10:21",
                     "comment": "I aim to adopt several approaches"
                     },{
                     "commentId": "3",
                     "person": "Callum Lindsay",
                     "act": "Comment",
                     "date": "18/12/2008 11:21",
                     "comment": "I can support you here Andy"
            }]
        }]
    };

  2. #2
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    82
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Something like:
    Code:
    var record = ....
    var oldComments = record.get('comments');
    var newComments = [].concat(oldComments);
    newComments.push({"commentId": "4", ...});
    record.set('comments', newComments);

  3. #3
    Sencha User
    Join Date
    Apr 2008
    Location
    West Linton, Scotland
    Posts
    244
    Vote Rating
    0
    andycramb is on a distinguished road

      0  

    Default


    something like
    I didn't need to do anything your code was spot on
    Thank you
    Andy

Thread Participants: 1