6 Replies Latest reply on Dec 30, 2007 1:39 PM by fi2itoxlay

    updated XML not showing between states

    fi2itoxlay
      Hi Gurus,

      I am fairly new to AS3/Flex and would like your help with an issue I am having. I am implementing a Flex 2 project that uses two custom components in different states (i.e. State 1 uses component A. State 2 uses component B). In these two components, they are referencing the same XML object that is specified in a seperate file (i.e. Global.as) that is included in the main application file.

      Here's my problem. If I switch between the two states making changes to the XML object, these changes are not shown in the states unless I force a new databind() by setting the dataprovider to the updated XML object. I could just do this everytime the user switches the state, however, I don't think this is an efficient way to solve the problem.

      I was wondering if you guys could help me find a better way to solve this problem. Could we set an event that triggers the reseting of the XML databind ONLY when the XML object is changed?

      Thanks.
      - Lay
        • 1. Re: updated XML not showing between states
          atta707 Level 2
          With a disclaimer that I'm not a guru, let me ask you if your XML variable is declared as [Bindable]?

          ATTA
          • 2. Re: updated XML not showing between states
            fi2itoxlay Level 1
            Yes. The following is what is in the Global.as file.

            [Bindable]
            public var carsXML:XML = new XML(
            <cars>
            <car make="Acura" model="RSX"/>
            <car make="Toyota" model="Camry"/>
            </cars>
            );

            The two states are either adding more "car" nodes or deleting them. I need the screens to reflects these changes to the XML object.

            Thanks in advance!
            - Lay
            • 3. Re: updated XML not showing between states
              atta707 Level 2
              Okay that's good.

              What kind of flex controls you're using to to show the contents of this XML list?

              Have you tried to move this XML out of common file into the MXML just to verify that's the problem?

              I think if you can set up a stripped down application that reproduces this behavior and paste it here, it will be helpful for other people 'see' the problem.

              ATTA
              • 4. Re: updated XML not showing between states
                fi2itoxlay Level 1
                Please use the following link to DL the files that will illustrate the issue I am having.

                www.redlionsolutions.com/downloads/xmlissue.zip

                You might be wondering why I have a "ParentComponent" with subcomponents on it. The reason for this is because my application will have 4 different categories: Cars, Buses, Trains, Airplanes. Each category will be a "ParentComponent" for the subcomponents that deals with the corresponding category type. The user will be able to navigate through each category by a header navigation (not included in the sample I provided).

                Please let me know if you need any other information. Thanks for your help.

                - L
                • 5. Re: updated XML not showing between states
                  atta707 Level 2
                  any particular reason you haven't wrapped you XML list to XMLListCollection? This is what flex does for you when you assign it a simple XML or XMLList element.

                  So in your Global.as right after:

                  public var carsXML:XML = new XML(
                  <cars>
                  <car id="1" make="Acura" model="RSX"/>
                  <car id="2" make="Honda" model="Accord"/>
                  <car id="3" make="Toyota" model="Camry"/>
                  </cars>
                  );

                  do a

                  public var carsXMLView:XMLListCollection = new XMLListCollection(carsXML.car);

                  and now you should be using carsXMLView everywhere you used to carsXML, like in DataGrid

                  <mx:DataGrid id="dgCars" x="24" y="46" width="544" height="169"
                  dataProvider="{Application.application.carsXMLView}">

                  and while adding a new car to the list:

                  var newObj:XML = <car id={Application.application.carId} make={tbMake.text} model={tbModel.text} />;
                  Application.application.carsXMLView.addItem(newObj);// .appendChild(newObj);

                  Hope this helps!

                  Please read up on dataProvider property for DataGrid class.

                  Also please note the Application.application.* is not really a recommended way of passing any data to different components! Use events, as you already have, or defined properties and set the data source.

                  ATTA

                  • 6. Re: updated XML not showing between states
                    fi2itoxlay Level 1
                    This works! Thank you ATTA.