Results 1 to 8 of 8

Thread: TreeNode: Multiple async calls creates duplicated children

  1. #1
    Sencha User
    Join Date
    Mar 2007
    Location
    Minneapolis, MN, USA
    Posts
    72

    Default TreeNode: Multiple async calls creates duplicated children

    all, I ran into a little bug that will probably rarely show itself, but i fixed it none-the-less.

    Scenario using the .40 code from svn:

    I had my debug process running on the script that TreeLoader calls to retrieve its data. this resulted in the UI spinning the little loading icon. I didnt realized that i had hit a breakpoint in visual studio and double clicked on the node multiple times while it was loading. Once i realized that i was at a breakpoint in the code, i continued through and to my surprise was presented with three copies of the children that were loaded were attached to the node.

    To fix this TreeLoader needs to remove existing children of the node before attaching the fresh results from the async call. I chose to write a removeAllChildren function to simplify things a bit. Code for various classes is presented below. What are other's thoughts?

    -------------------------------------------------------------------

    modified YAHOO.ext.tree.TreeLoader.processResponse

    Code:
    processResponse : function(response, node, callback){
            var json = response.responseText;
            try {
                var o = eval('('+json+')');
                node.removeAllChildren();
    	        for(var i = 0, len = o.length; i < len; i++){
    	            node.appendChild(this.createNode(o[i])); 
    	        }
    	        if(typeof callback == 'function'){
                    callback();
                }
            }catch(e){
                this.handleFailure(response);
            }
        }


    added function to YAHOO.ext.tree.TreeNode

    Code:
    removeAllChildren : function(){       
            var cs = this.childNodes;
            for(var i = 0, len = cs.length; i < len; i++){
                // unselect nodes
                this.ownerTree.getSelectionModel().unselect(cs[i]);
                
                if(this.childrenRendered){
                    // if it's been rendered remove dom node
                    node.ui.remove();
                }
            }
            
            YAHOO.ext.tree.TreeNode.superclass.removeAllChildren.apply(this, arguments);
            
            this.collapse(false, false);
            return;
        }


    added function to YAHOO.ext.data.Node

    Code:
    removeAllChildren : function(){
            if(this.fireEvent('beforeremove', this.ownerTree, this, null) === false){
                return false;
            }
                
            this.childNodes = [];
    
            this.fireEvent('remove', this.ownerTree, this, null);
            return;
        }


    is this something that can be added to core files in svn? i've never been a part of an "open source" community and am not sure if that is something that I contribute to svn directly or if I prompt the need for the fix here and have jack review it and make the change.

    -j

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    6,956

    Default

    For removeAll, it's the same as standard DOM:

    Code:
    while(node.firstChild){
        node.removeChild(node.firstChild);
    }
    The associated events will do the rest (unselect, remove html, etc).

    Blanking the node on load will have to be an option, because people who have created the tree from existing markup or manually created nodes may have nodes there they want to keep. I've added a clearOnLoad option that defaults to true.

    The actual code modification was a little shorter, this went on the beginning of the load method:

    Code:
    load : function(node, callback){
            if(this.clearOnLoad){
                while(node.firstChild){
                    node.removeChild(node.firstChild);
                }
            }
            ....
    I would like to note that loading multiple times in a normal environment (not in a debugger) should not be possible.

  3. #3
    Sencha User
    Join Date
    Mar 2007
    Location
    Minneapolis, MN, USA
    Posts
    72

    Default

    regarding the possibilty of this happening...agreed.
    couldnt it theoretically happen with an async call that is intensive (lots of data processing). granted...how intensive can processing the child nodes of a menu item be?

    thanks jack for hearing the pee-ons
    -j

  4. #4
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    6,956

    Default

    As long as you aren't somehow doing the load call outside of the tree, it shouldn't happen. If you look at AsyncTreeNode, it queues the callback for multiple calls and only allows loading once.

  5. #5
    Sencha User
    Join Date
    Mar 2007
    Location
    Minneapolis, MN, USA
    Posts
    72

    Default

    simply genius

  6. #6
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    6,956

    Default

    Ah actually this made me open the code and look and boom! The queueing block is missing one thing very important, a return statement at the end. It should be right after the call to setInterval.
    Code:
    ...
    timer = setInterval(f, 200);
    return;
    Without that return, the call gets queue'd but the normal expand code still gets executed!

    I'm glad this thread continued because that would have been a hard one to figure out!

  7. #7
    Sencha User
    Join Date
    Mar 2007
    Location
    Minneapolis, MN, USA
    Posts
    72

    Default

    so with this, it makes the clearOnLoad config option completely obsolete, right?

  8. #8
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    6,956

    Default

    Maybe, still makes sense to have it in there though I guess.

Similar Threads

  1. Need help loading treeNode with children data
    By simeon in forum Ext 1.x: Help & Discussion
    Replies: 4
    Last Post: 21 Mar 2009, 8:35 PM
  2. async treenode expand/load bug
    By seldon in forum Ext 1.x: Bugs
    Replies: 1
    Last Post: 14 Feb 2007, 1:31 PM
  3. multiple calls to contentPanel.setUrl()
    By snid in forum Ext 1.x: Bugs
    Replies: 0
    Last Post: 9 Feb 2007, 1:24 PM
  4. setTimeout calls yui or yui-ext class member
    By dlibby00 in forum Ext 1.x: Help & Discussion
    Replies: 2
    Last Post: 7 Feb 2007, 8:27 AM
  5. XMLDataModel and Parent/Children Nodes
    By vatech1993 in forum Ext 1.x: Help & Discussion
    Replies: 9
    Last Post: 27 Nov 2006, 2:32 PM

Posting Permissions

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