1 Reply Latest reply on Sep 8, 2009 7:52 AM by Andrew Rosewarn

    Best practice for lazy-loading collection once but making sure it's there?

    rickcr Level 1

      I'm confused on the best practice to handle the 'setup' of a form, where I need a remote call to take place just once for the form, but I also need to make use of this collection for a combobox that will change when different rows in the datagrid or clicked. Easier if I just explain...


      1. You click on a row in a datagrid to edit an object (for this example let's say it's an "Employee")
      2. The form you go to needs to have a collection of "Department" objects loaded by a remote call. This collection of departments only should happen once, since it's not common for them to change. The collection of departments is used to populate a form combobox.
      3. You need to figure out which department of the comboBox is the selectedIndex by iterating over the departments and finding the one that matches the employee.department.id


      Individually, I know how I can do each of the above, but due to the asynch nature of Flex, I'm having trouble setting up things. Here are some issues...


      My initial thought was just put the loading of the departments in an init() method on the employeeForm which would load as creationComplete() event on the form. Then, on the grid component page when the event handler for clicking on a row was fired, I call a setup() method on my employeeForm which will figure out which selectedIndex to set on the combobox by looking at the departments.


      The problem is the resultHandler for the departments load might not have returned (so the departments might not be there when 'setUp' is called), yet I can't put my business logic to determine the correct combobox in the departmentResultHandler since that would mean I'd always have to fire the call to the remote server object every time which I don't want.


      I have to be missing a simple best practice? Suggestions welcome.

        • 1. Re: Best practice for lazy-loading collection once but making sure it's there?
          Andrew Rosewarn Level 3

          Hi there rickcr


          This is pretty rough and you'll need to do some tidying up but have a look below.


          <?xml version="1.0"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
                      import mx.controls.Alert;
                      import mx.collections.ArrayCollection;
                      private var comboData:ArrayCollection;
                      private function setUp():void {
                          if (comboData) {
                              Alert.show('Data Is Present')
                          } else {
                              Alert.show('Data Not')
                      private function getData():void {
                          comboData = new ArrayCollection();
                          // On the result of this call the setUp again
                      private function populateForm():void {
                          // populate your form
              <mx:TabNavigator left="50" right="638" top="50" bottom="413" minWidth="500" minHeight="500">
                  <mx:Canvas label="Tab 1" width="100%" height="100%">
                  <mx:Canvas label="Tab 2" width="100%" height="100%" show="setUp()">


          I think this example is kind of showing what you want.  When you first click tab 2 there is no data.  When you click tab 2 again there is. The data for your combo is going to be stored in comboData.  When the component first gets created the comboData is not instansiated, just decalred.  This allows you to say


          if (comboData)


          This means if the variable has your data in it you can populate the form.  At first it doesn't so on the else condition you can call your data, and then on the result of your data coming back you can say


          comboData = new ArrayCollection(), put the data in it and recall the setUp procedure again.  This time comboData is populayed and exists so it will run the populate form method and you can decide which selected Item to set.


          If this is on a bigger scale you'll want to look into creating a proper manager class to handle this, but this demo simple shows you can test to see if the data is tthere.


          Hope it helps and gives you some ideas.