10 Replies Latest reply on Aug 19, 2008 12:12 PM by Newsgroup_User

    Variables and Main

    GeorgeWS Level 1
      I have a main.mxml

      I have this:

      public function alertClickHandler(event:CloseEvent):void {
      Alert.show("Items:",tempStr);
      }

      I have a Cart.mxml. I collect the items like this:

      public var cartAryCol:ArrayCollection ;
      private function initVars():void {
      cartAryCol = new ArrayCollection();
      }

      private function addToCart(event:DragEvent):void {
      var items:Array = event.dragSource.dataForFormat("items") as Array;
      var aBookOrder:BookOrder = new BookOrder();
      aBookOrder.PNUM5W = items[0].PNUM5W ;
      cartAryCol.addItem( aBookOrder );
      }

      public function checkOut():void {
      var tempStr:String = "Your cart contains: \n";
      for (var i:int = 0; i < cartAryCol.length; i++) {
      parentApplication.tempStr = tempStr + cartAryCol .PNUM5W + "\n" ;
      }
      }

      I want to have the alert on the main.mxml show what was in the cartAryCol. If I do this:
      Alert(tempStr) this file will show the alert fine. I just want it to be shown by the main.mxml so I can use the string.

      Anyone?

      Thanks
      George
        • 1. Re: Variables and Main
          Bob Saggot Level 1
          What I usually do in a situation like that, is add an event listener for the checkout button in main so

          Assuming cartcheckout.mxml id is "thecart"
          and assuming the id of the checkout button IN cart is "checkoutbutton"

          --ALL OF THIS IS IN MAIN--

          creationComplete="startup()"

          private function startup():void
          {
          thecart.checkoutbutton.addeventListener(MouseEvent.Click,"showpopup");
          }

          private function showpopup(event:mouseevent):void
          {
          var thecomponent:Canvas = Canvas(event.parentDocument); // assuming cart.mxml is a canvas or whatever it is
          var temp:ArrayCollection = thecomponent.thearraycollection; //grabs the cart data;

          //temp is now all the cart data located in your main, ready for a popup

          }

          sorry for the ugly code, I can't explain stuff well but hopefully this helps you.
          • 2. Re: Variables and Main
            GeorgeWS Level 1
            Thanks Bob,
            I have a couple of questions though. Im not sure what you mean by this:
            Assuming cartcheckout.mxml id is "thecart"
            Is there another mxml file that I need? and what do you mean by the Canvas. I have a DataGrid with id="dgSelected" is that what I should be using?
            The rest seems to make some scence to me, thanks

            George
            • 3. Re: Variables and Main
              Bob Saggot Level 1
              your Cart.mxml is a component i'm assuming, and just for coding purposes i gave it the id of "thecart"

              so in your main app it would be something like

              <ns1:Cart id="thecart">
              </Cart>
              and what I meant by Canvas, is whatever component you derived "Cart.mxml" from.

              In fact it would just make sense to put this.
              var thecomponent:Cart = Cart(event.parentDocument);

              you're taking whatever cart you're currently using and making it readily available inside your main app. So you can access all of its components.

              thecomponent.theArrayCollection.getItemAt(0).whatever
              • 4. Re: Variables and Main
                GeorgeWS Level 1
                Thanks for the help. I am not smart. I get a bunch of errors, but I think its sort of sinkining in. My component call on the main.mxml is like this:
                <custom:Cart id="cart" width="100%" height="100%"/>

                and I added this to the main.mxml, I also have the creationComplete part.

                private function startup():void
                {
                cart.checkOutBtn.addeventListener(MouseEvent.Click,"showpopup");
                }

                private function showpopup(event:MouseEvent):void
                {
                var thecomponent:Cart = Cart(event.parentDocument);
                var temp:ArrayCollection = thecomponent.tempStr;
                }

                tempStr is created when the button - checkOutBtn is pressed on the Cart.mxml
                My Cart.mxml look like this to collect the dragged item into the datagrid.

                public var cartAryCol:ArrayCollection ;

                private function initVars():void {
                cartAryCol = new ArrayCollection();
                }

                private function addToCart(event:DragEvent):void {
                var items:Array = event.dragSource.dataForFormat("items") as Array;
                var aWineOrder:WineOrder = new WineOrder();
                aWineOrder.PNUM5W = items[0].PNUM5W ;
                cartAryCol.addItem( aWineOrder );
                }

                public function checkOut():void {
                var tempStr:String = "Your cart contains: \n";
                for (var i:int = 0; i < cartAryCol.length; i++) {
                tempStr = tempStr + cartAryCol .PNUM5W + "\n" ;
                }
                Alert.show( tempStr );
                }

                The errors that I get say "undefined property for Click... Undefined method addeventListener
                Undefined property tempStr...

                I did change the (event:mouseevent) to (event:MouseEvent)
                Do I need to import something
                Thanks

                George

                George
                • 5. Re: Variables and Main
                  Level 7

                  "GeorgeWS" <webforumsuser@macromedia.com> wrote in message
                  news:g6snr7$67h$1@forums.macromedia.com...
                  > Thanks for the help. I am not smart. I get a bunch of errors, but I think
                  > its
                  > sort of sinkining in. My component call on the main.mxml is like this:
                  > <custom:Cart id="cart" width="100%" height="100%"/>
                  >
                  > and I added this to the main.mxml, I also have the creationComplete part.
                  >
                  > private function startup():void
                  > {
                  > cart.checkOutBtn.addeventListener(MouseEvent.Click,"showpopup");
                  > }
                  >
                  > private function showpopup(event:MouseEvent):void
                  > {
                  > var thecomponent:Cart = Cart(event.parentDocument);
                  > var temp:ArrayCollection = thecomponent.tempStr;
                  > }

                  temp is an ArrayCollection, but you are trying to set it to a string. Not
                  only that, but, as its name suggests, tempStr is a temporary variable with
                  no existence outside the checkOut() function.

                  > public function checkOut():void {
                  > var tempStr:String = "Your cart contains: \n"; <declared
                  > inside your function
                  > for (var i:int = 0; i < cartAryCol.length; i++) {
                  > tempStr = tempStr + cartAryCol .PNUM5W + "\n" ;
                  > }
                  > Alert.show( tempStr );
                  > }


                  • 6. Re: Variables and Main
                    GeorgeWS Level 1
                    Im stumped, this should be easy like using a session variable in CF. When I press the button that is located in the cart.mxml an alert pops up and shows the items in the cart. I just want to use those items in the main.mxml. How do you do this?

                    Thanks
                    George
                    • 7. Re: Variables and Main
                      Level 7

                      "GeorgeWS" <webforumsuser@macromedia.com> wrote in message
                      news:g8cc5a$fa2$1@forums.macromedia.com...
                      > Im stumped, this should be easy like using a session variable in CF. When
                      > I
                      > press the button that is located in the cart.mxml an alert pops up and
                      > shows
                      > the items in the cart. I just want to use those items in the main.mxml.
                      > How do
                      > you do this?

                      Try getting off your _ and checking out some of the leads you've already
                      been given!


                      • 8. Re: Variables and Main
                        GeorgeWS Level 1
                        Thanks for that. You are no help at all. I have the books and have looked at the "Leads". If they where clear I would not be asking the questions here. Anyone else that wants to help me would be nice.

                        G.
                        • 9. Re: Variables and Main
                          GeorgeWS Level 1
                          I have gotten a liitle further. Just as notes... I have a main.mxml and a Cart.mxml; in the Cart I have this:

                          public function checkOut():void {
                          var tempStr:String = "Does this work?: \n";
                          for (var i:int = 0; i < cartAryCol.length; i++) {
                          tempStr = tempStr + cartAryCol .PNUM5W + "\n" ;
                          }
                          }

                          It gathers items that are droped into a datagrid in the Cart.mxml (works great)

                          On the Main.mxml I have this:

                          public function startup():void
                          {
                          cart.checkOutBtn.addEventListener(MouseEvent.CLICK, showpopup);
                          }

                          It listens for the button that is on the Cart.mxml to be pressed an then firest this:

                          public function showpopup(event:MouseEvent):void
                          {
                          var cart:Cart = Cart(event.parentDocument);
                          var temp:String = cart.tempStr;
                          Alert.show( temp );
                          }

                          This part I cannot get to work. The Alert pops up and is blank. When I clean the project I get and error saying "undefined property parentDocument through a ref. with static type flash.events:MouseEvent" Im not sure what this means.
                          Is the parentDocument the command for trying to reach down to a child to see a variable?

                          Thanks

                          George
                          • 10. Re: Variables and Main
                            Level 7

                            "GeorgeWS" <webforumsuser@macromedia.com> wrote in message
                            news:g8f2vi$2a8$1@forums.macromedia.com...
                            >I have gotten a liitle further. Just as notes... I have a main.mxml and a
                            > Cart.mxml; in the Cart I have this:
                            >
                            > public function checkOut():void {
                            > var tempStr:String = "Does this work?: \n";
                            > for (var i:int = 0; i < cartAryCol.length; i++) {
                            > tempStr = tempStr + cartAryCol .PNUM5W + "\n" ;
                            > }
                            > }
                            >
                            > It gathers items that are droped into a datagrid in the Cart.mxml (works
                            > great)
                            >
                            > On the Main.mxml I have this:
                            >
                            > public function startup():void
                            > {
                            > cart.checkOutBtn.addEventListener(MouseEvent.CLICK, showpopup);
                            > }
                            >
                            > It listens for the button that is on the Cart.mxml to be pressed an then
                            > firest this:
                            >
                            > public function showpopup(event:MouseEvent):void
                            > {
                            > var cart:Cart = Cart(event.parentDocument);
                            > var temp:String = cart.tempStr;
                            > Alert.show( temp );
                            > }
                            >
                            > This part I cannot get to work. The Alert pops up and is blank. When I
                            > clean
                            > the project I get and error saying "undefined property parentDocument
                            > through a
                            > ref. with static type flash.events:MouseEvent" Im not sure what this
                            > means.
                            > Is the parentDocument the command for trying to reach down to a child to
                            > see a
                            > variable?
                            http://www.adobe.com/devnet/flex/articles/graduating_pt1.html