1 Reply Latest reply on Jun 30, 2008 9:36 PM by francois-yanick

    Passing variable to an outside component

    francois-yanick Level 1
      I intent to create a web site of products. Since there will be too many lines of code to work only with one file, I decided to create component for each state. Those component are simply VBox component including everything necessary to display the nature of the state. For an example, calling the state local:stateContactComp will show the state for contact (equivalent to a page of contact from the web 1.0 philosophy if I can say it in that way).

      Now, the problem I encounter at this moment is the current product. The home page is designed to navigate through the products available and once the user click on one product, he/she will be redirected to the state of product where the system will call the stateProductComp.mxml based on a VBox (but followed by many component inside to show the details of the product). This is basic but what I don't know yet is how to pass the current product (the one that the user clicked on) to the component which is called. At the beginning I sort of though that the component called was automatically binded to the main component but it seems to not. I alreay declared a public var currentProduct:Product in my stateProductComp.mxml but I still searching how to transfer the information.

      Preferably, I would prefer to use the main variable which have a similar line (public var currentProduct:Product) associated to a class to define the Product. I would prefer to use the main variable for memory worry but maybe there is no way to take care of the memory in this case (which I doubt!).

      Anyway, what I need to know is the best practice to work with separate files (for lenght of code source issue) while we are working with states.

      I hope I was enough clear to understand my problem, if not, please let me know.

      Keywords should be: passing a variable to a component in another file.
        • 1. Passing variable to an outside component
          francois-yanick Level 1
          I knew it was possible to do it and I found how!!!

          Simply by adding the value through the call of the component.

          In my case, I was calling the component while changing state so the line of code was as follow:

          1) <mx:State name="stateProduct">
          2) <mx:RemoveChild target="{menu2Items}"/>
          3) <mx:AddChild relativeTo="{pageContent}" position="lastChild" creationPolicy="all">
          4) <mx:VBox id="pageContent2" width="100%" height="100%">
          5) <local:stateLampeComp currentProduct="{vCurrentProduct}"/>
          6) </mx:VBox>
          7) </mx:AddChild>
          8) </mx:State>

          So the line 5 show my call to the component containing all the layout to display the details of the product pre-clicked and by indicate currentProduct (variable from the stateLampeComp component) = {vCurrentProduct} (variable from the caller component), the system can now inter-connecting the values from one component to the other.

          Voilà!