5 Replies Latest reply on May 22, 2009 9:21 AM by phillipus_rex

    Please explain this code to me (in English)!  Thanks.

    phillipus_rex Level 1

      Hello everyone,

       

      I am working with some AS3 code that someone else has written.  The code creates datatables and populates them by pulling from a .NET webservice using SOAP and DataTables.

       

      I have copied and pasted the code below.  Most of it makes sense and I understand it, however, there are some things I've never seen before and have not been able to find on the internet.  Most of the things I have a problem with, I've created comments in ALL CAPS with my questions.

       

      If someone could take a look at it and tell me what is going on I would appreciate it.

       

      Thanks.

       

       

       

                    //Load result data into XMLList structure
                    var xmlData:XML = XML(myService.GetViewResults.lastResult);
                    
                    //gets the DataTables data, length of tableXML is the number of Tables you got
                    var tableXML:XMLList = xmlData.GetViewResultsResult.DataTables.children();
                                  
                    //gets the number of tables returned in the result
                    var numTables:int = tableXML.children().length();
                    
                    //seperates out the data for each table
                    var tempXMLArray:Array = new Array();
                    var tempXML:XML;
                    for (var i:int=0; i<numTables; i++)
                    {
                        tempXML = tableXML[i];
                        tempXMLArray.push(tempXML);
                    }
                    
                    //create a datagrid for each table
                    var datagrid1:DataGrid, datagrid2:DataGrid, datagrid3:DataGrid;
                    
                    //create array collections to feed datagrids
                    var ac1:ArrayCollection, ac2:ArrayCollection, ac3:ArrayCollection;
                    
                    var currentTableXML:XML;
                    var columns:Array = new Array();
                    var dgc:DataGridColumn;
                    var obj:Object;  // WHY IS THIS OBJECT NEEDED?
                    
                    //CREATING TABLE 1
                    currentTableXML = tempXMLArray[0];
                    
                    datagrid1 = new DataGrid();
                    datagrid1.width = 1000;
                    datagrid1.height = 200;
                    datagrid1.y = 0;
                    
                    columns = new Array();
                    for (i=0; i<currentTableXML.Columns.children().length(); i++)
                    {
                        dgc = new DataGridColumn(currentTableXML.Columns.NHRCViewResultColumn.ColumnName[i].toString());
                        dgc.dataField = currentTableXML.Columns.NHRCViewResultColumn.ColumnName[i];
                        columns.push(dgc);
                    }
                
                    datagrid1.columns = columns;
                    
                    ac1 = new ArrayCollection;
                    
                      
                     // looping through each piece of data in the row
                     for (i=0; i<currentTableXML.Rows.children().length(); i++)
                    {
                        trace("table 1:creating row " + i);
      
                     // I HAVE NO IDEA WHATS GOING ON HERE... WHAT IS THIS OBJECT DOING EXACTLY?
                        obj = {
                               // I DON'T UNDERSTAND THIS SYNTAX - WHAT ARE THE COLONS FOR???  AND WHY STORE IN AN OBJECT???
                            ((datagrid1.columns[0] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.Values[i].string[0]),
                            ((datagrid1.columns[1] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.Values[i].string[1]),
                            ((datagrid1.columns[2] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.Values[i].string[2]),
                            ((datagrid1.columns[3] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.Values[i].string[3]),
                            ((datagrid1.columns[4] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.Values[i].string[4]),
                            ((datagrid1.columns[5] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.Values[i].string[5])
                        };
                        
                        ac1.addItem(obj);
                    }
                    
                    datagrid1.dataProvider = ac1;
                    this.addChild(datagrid1);  //Adding populated datagrid to the screeen
                    
        • 1. Re: Please explain this code to me (in English)!  Thanks.
          Gregory Lafrance Level 6

          The following code creates a variable obj of type Object. You need to declare the variable, and you don't want to do that inside the loop where the object is constructed, because then it will be scoped within the loop block, so it is first declared outside the loop.

           

          var obj:Object;  // WHY IS THIS OBJECT NEEDED?
          

           

           

          Now you are constructing the object. Here is the logic of what is going on.

           

          Objects of this type in Flex are constructed as follows:

           

          objName = {

            fieldName1: fieldVal1,

            fieldName2: fieldVal2,

            fieldName3: fieldVal3

          };

           

          In this code, the field names are:

          (datagrid1.columns[0] as DataGridColumn).dataField.toString()
          

          and you need to cast as DataGridColumn first.

           

          The field values are:

           

          (currentTableXML.Rows.NHRCViewResultRow.Values[i].string[0])
          

           

          So here it seems you are taking the first item in the string element of the ith element of Values, which is obtained by parsing into the currentTableXML, Rows element, NHRCViewResultRow element.

           

          // I HAVE NO IDEA WHATS GOING ON HERE... WHAT IS THIS OBJECT DOING EXACTLY?
                            obj = {
                                   // I DON'T UNDERSTAND THIS SYNTAX - WHAT ARE THE COLONS FOR???  AND WHY STORE IN AN OBJECT???
                                ((datagrid1.columns[0] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.V alues[i].string[0]),
                                ((datagrid1.columns[1] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.V alues[i].string[1]),
                                ((datagrid1.columns[2] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.V alues[i].string[2]),
                                ((datagrid1.columns[3] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.V alues[i].string[3]),
                                ((datagrid1.columns[4] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.V alues[i].string[4]),
                                ((datagrid1.columns[5] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.V alues[i].string[5])
                            };
          
          
          • 2. Re: Please explain this code to me (in English)!  Thanks.
            Michael Borbor Level 4

            Greg if I'm not wrong in AS3 when you define a var inside a function it doesn't matter where in the function it's declare it'll be available for all the function.

            • 3. Re: Please explain this code to me (in English)!  Thanks.
              Gregory Lafrance Level 6

              That could be. So in that case the declaration may be technically unnecessary.

              • 4. Re: Please explain this code to me (in English)!  Thanks.
                Michael Borbor Level 4

                Yeah probably a code convention, declare variables at the top of the function.

                • 5. Re: Please explain this code to me (in English)!  Thanks.
                  phillipus_rex Level 1

                  Thanks Greg it makes a lot more sense now!

                   

                  Now lets say I wanted to create an ArrayCollection (like ac2 in the previous code) that only contains one of the items in that object, like:

                   

                  ((datagrid1.columns[5] as DataGridColumn).dataField.toString()):(currentTableXML.Rows.NHRCViewResultRow.V alues[i].string[5])

                   

                   

                  How can I do this and remove all of the datagrid stuff to make it more generic?  For instance, so that I could populate a ComboBox or use the ArrayCollection anywhere?

                   

                  Thanks again.