3 Replies Latest reply on Feb 26, 2010 10:07 AM by Gregory Lafrance

    Run a method next frame?

    Eric_Seg

      Hi.  I'm new to Flex.  I want to expand the root of a TreeList with myTree.expandChildrenOf(myTree.dataProvider[0], true) but this does not work for one of two reasons:  either the [0] node doesn't exist yet (even though I just loaded it with data), or the tree draws the default (collapsed) view immediately after this line.  Is there a way to delay execution until the next frame?  Is there an event I can use?  Is there an expanded property I can set on the root node in the dataProvider?  I'm using Flex 3.3.  Thanks.

        • 1. Re: Run a method next frame?
          Gregory Lafrance Level 6

          From the Flex Builder help system:

           

          Opens or closes all the tree items below the specified item. If you set dataProvider and then immediately call expandChildrenOf() you may not see the correct behavior. You should either wait for the component to validate or call the validateNow() method.

          So wherever you are calling expandChildrenOf(), call validateNow() before you call expandChildrenOf().

           

          If this post answers your question or helps, please mark it as such.


          Greg Lafrance - Flex 2 and 3 ACE certified

          www.ChikaraDev.com

          Flex / AIR Development, Training, and Support Services

          1 person found this helpful
          • 2. Re: Run a method next frame?
            Eric_Seg Level 1

            Thank you, Greg.  I think validateNow() was the method I was thinking of.  I ran across it once before.

             

            Unfortunately, it is still not working. I get "index [0] specified is out of bounds."  Tracing there shows just this, and validateNow() does not change this.  After the error, all my data shows in the (collapsed) tree.  Running the code a second time flickers the tree expanded, then collapsed again (with no errors).

             

            Here is a snippet of my code.  myTreeResults is a module that contains a tree in a panel:

             

            myTreeResults.LoadData();

            myTreeResults.visible = true;

            myTreeResults.ExpandRoot();

             

            Code for myTreeResults:

             

            public function LoadData():void {
                myRemoteObject.getResults();   
            }

            public function ExpandRoot():void {
                this.myTree.validateNow();
                this.myTree.expandChildrenOf(myTree.dataProvider[0], true);
            }

             

            Any ideas what I'm doing wrong?

            • 3. Re: Run a method next frame?
              Gregory Lafrance Level 6

              I think the problem may have to do with the fact that Flex is asynchronous.

               

              You call myTreeResults.LoadData() and then soon thereafter you call myTreeResults.ExpandRoot(), but because Flex is asynchronous the data may not be loaded yet.

               

              You would be better off calling myTreeResults.ExpandRoot() in the data loading result handler, or in whatever function loads the data, right after you have put the data in the dataprovider.

               

              If you are using "lastResult" then you need to make changes to use a ResultEvent handler.

               

              If this post answers your question or helps, please mark it as such.


              Greg Lafrance - Flex 2 and 3 ACE certified

              www.ChikaraDev.com

              Flex / AIR Development, Training, and Support Services