5 Replies Latest reply on Jan 4, 2008 8:17 AM by peterent

    Flex viewstack question

    E M Bronc
      My default creationpolicy for my viewstack causes each child component to be created only when a user navigates to it. This is good for performance. I was wondering if there was a way to have it recreate itself each time a user comes back to that particular view. Also is there a way to explicitly call a recreate on a particular child in a viewstack?
      Thanks
        • 1. Re: Flex viewstack question
          peterent Level 2
          What do you mean by "recreate" and why would you want to do this? Generally the children of the navigation containers (ViewStack, Accordion, etc.) are repopulated with different data and there is no need to recreate them. If you really, really want to do this, then I think the easiest way is to do it like this:

          <mx:ViewStack id="stack" ... >
          ...<MyPanel />
          ...<MyBox />
          ...<MyForm />
          </mx:ViewStack>

          Support you want to recreate the second child, MyBox. I would first remove it from the ViewStack:

          stack.removeChildAt(1);

          and then create it again:

          myBox = new MyBox();
          stack.addChildAt(myBox,1);
          • 2. Re: Flex viewstack question
            E M Bronc Level 1
            You mentioned that the children are "repopulated" with different data. How exactly is this done without recreating them?
            • 3. Re: Flex viewstack question
              peterent Level 2
              Let's say the child component in question displays the information about a house for sale: the price, square footage, number of bedrooms, etc. In this component you would have Label components and either Text or TextInput fields to hold the values:

              Price: $175,000

              In Flex you could code this as: <mx:Label text="Price" /> <mx:Text text="{price}" />

              Here, the price is a variable in the component which is bound to the Text component using the Flex binding notation of { and }. You declare price (and all of the other values) using:

              [Bindable] public var price:Number;
              [Bindable] public var bedrooms:Number;
              // etc.

              To repopulate this component with new data you just set the variables:

              <mx:ViewStack ..>
              ....<AgentInfo id="agentInfo" ../.>
              ....<RealEstateInfo id="realInfo" .../ >
              ...
              </mx:ViewStack>

              realInfo.price = 200000; or realInfo.price = dataFromServer.price;

              You set up some variables in the component to hold your data and bind those variables to the UI controls. Whenever you change the variables the values on the screen change.

              The beauty of data binding is that you don't need to have the component created - the ViewStack by default only creates the contents of its first child. If this realInfo child is the second or third, the controls within it won't be created until the user visits it. But the variables are created so you can change them and data binding will take care of the rest.

              There is a simpler way to do the binding. Every component has a "data" property. Let's say you have an ActionScript class that represents the information about a house (House class). Rather than doing:

              realInfo.price = house.price;
              realInfo.bedrooms = house.bedrooms;
              etc.

              You can do:
              realInfo.data = house;

              and then instead of <mx:Text text="{price}" /> in the component, do <mx:Text text="{data.price}" />
              • 4. Re: Flex viewstack question
                E M Bronc Level 1
                Thank you for your response. The reason for this scenario is I have an applicationcontrolbar fixed at the top of my application. There is a dropdown there and depending on what is selcted there data will be affected in all the children of the viewstack. If a user goes into into several children of the viewstack then changes the value of the combobox at the top of the app i need to make sure if he goes back into those same children the data is changed.
                • 5. Re: Flex viewstack question
                  peterent Level 2
                  Excellent. Then what I'm suggesting is perfect for what you want to do.