1. #1
    Sencha Premium Member
    Join Date
    May 2007
    Location
    USA
    Posts
    73
    Vote Rating
    0
    notjoshing is on a distinguished road

      0  

    Default saving a nested tree

    saving a nested tree


    I am using a tree to represent a nested architecture which I'd like to save off to a database. The architecture works something like book categorizations: there are multiple sections, such as fiction and non-fiction. Fiction, in turn, has many genres. Each genre has many authors, and each author may have one or more books.

    I'd like to save the nodes in the tree sequentially from the top down. I tried doing this with recursion, but couldn't figure out a way to get the JS to wait for a response from the server before submitting its next request. That ended up causing deadlocks.

    I trimmed the code from the recursion function below. Thanks in advance for any help.

    Josh

    PHP Code:
    function saveNodeValuesOrig(node) {
        
    ////////////////////
        // Setting up values
        ////////////////////
        // Save form
        
    Ext.Ajax.request({
            
    urlsetLocation(),
            
    formform,
            
    success: function(data) {
                var 
    val         data.responseText;
                
    form[id].value  val;
                
    // child form
                
    var na          node.attributes;
                var 
    childForm   Ext.getDom('save'+
                    
    uc1st(na.childType));
                if( 
    childForm[id] ) { childForm[id].value val; }
                
    // call children
                
    var kids    node.childNodes;
                if( 
    kids.length != nodeFields[type]['size'] ) {
                    for(var 
    i=0i<kids.lengthi++) {
                        var 
    kid kids[i];
                        if( 
    kid.isLeaf() ) { continue; }
                        var 
    returnVal   saveNodeValues(kid);
                        if( 
    returnVal==false ) return false;
                    }
                }
                
    saveFlag    0;
            },
            
    failure: function() {
                
    alert"Error saving collection "+type+"s!" );
                return 
    false;
            }
        });
        return 
    true;


  2. #2
    Sencha - Architect Dev Team aconran's Avatar
    Join Date
    Mar 2007
    Posts
    9,092
    Vote Rating
    113
    aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold aconran is a splendid one to behold

      0  

    Default


    I suggest following this thread on the TreeSerializer plugin for trees developed by Animal and JeffHowden here on the forums.

    http://extjs.com/forum/showthread.ph...TreeSerializer

    In addition you should also look at the handy cascade and eachChild methods which you can find in the TreeNode doc's.
    http://extjs.com/deploy/ext/docs/out...e.html#cascade
    http://extjs.com/deploy/ext/docs/out...html#eachChild

    Aaron

  3. #3
    Sencha Premium Member
    Join Date
    May 2007
    Location
    USA
    Posts
    73
    Vote Rating
    0
    notjoshing is on a distinguished road

      0  

    Default


    Quote Originally Posted by aconran View Post
    I suggest following this thread on the TreeSerializer plugin for trees developed by Animal and JeffHowden here on the forums.

    http://extjs.com/forum/showthread.ph...TreeSerializer

    In addition you should also look at the handy cascade and eachChild methods which you can find in the TreeNode doc's.
    http://extjs.com/deploy/ext/docs/out...e.html#cascade
    http://extjs.com/deploy/ext/docs/out...html#eachChild

    Aaron
    Hi Aaron:

    My sense of using cascade or eachChild was that, if used to submit requests to the server, the asynchronous nature of those requests would result in the same deadlock situation I had earlier. Submitting JSON back to the server, though, or using TreeSerializer, both look like good options. I'll look into both today.

    Thanks,

    Josh

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

      0  

    Default


    If it's asynchronously loaded through Ajax, then you will have a problem serializing it because the whole tree will not be there in the browser to be serialized, and expand(true) (which expands all child nodes) does not wait for all children to be expanded before calling the callback.

    To be able to submit the full tree for processing on the server, it will need to be loaded in one go from a large object, and then fully expanded, then collapsed again to ensure that it's fully loaded.

  5. #5
    Sencha Premium Member
    Join Date
    May 2007
    Location
    USA
    Posts
    73
    Vote Rating
    0
    notjoshing is on a distinguished road

      0  

    Default


    Thanks all. I used a variant of the toJsonString method JeffHowden wrote to dump the tree to a string, which I fed to the server, digested with Perl, and saved to the database. Works like a charm, and plenty of time to pretty things up for Monday's meeting.

    Josh

  6. #6
    Sencha Premium Member
    Join Date
    May 2007
    Location
    USA
    Posts
    73
    Vote Rating
    0
    notjoshing is on a distinguished road

      0  

    Default


    Quote Originally Posted by Animal View Post
    If it's asynchronously loaded through Ajax, then you will have a problem serializing it because the whole tree will not be there in the browser to be serialized, and expand(true) (which expands all child nodes) does not wait for all children to be expanded before calling the callback.

    To be able to submit the full tree for processing on the server, it will need to be loaded in one go from a large object, and then fully expanded, then collapsed again to ensure that it's fully loaded.
    What I'm doing now is expanding and collapsing the tree after initially populating the node, and only adding regular nodes thereafter. This seems to control the behavior.

    Josh

Thread Participants: 2