7 Replies Latest reply on Jun 26, 2010 9:45 PM by John Hall

    Event Bubbling Question...

    jerry98225

      Hi guys

         I am trying to click two different button with the same handler. I want to assign the value in the 2 textArea above each button. I believe I can use event bubble to complete the quest, but after many tries, still can't get it to work. I appreciate any tips from you guys.

       

      My AS

       

      protected function uploadLargePic_clickHandler(event:MouseEvent):void

      {

       

       

       

           if(event.currentTarget.name=="picture"){

           Alert.show("yaaa");

           }

       

       

           if(event.currentTarget.name=="pictureBig"){

           Alert.show("ya Again!!!");

           }

       

      }

       

      MXML

       

      <mx:FormItem>

      <s:TextInput id="picture" width="100"/>

      <s:Button id="uploadPic" label="Browse.." click="uploadLargePic_clickHandler(event)"/>

      </mx:FormItem>

       

      <mx:FormItem>

      <s:Label text="Upload Large Picture"/>

      <s:TextInput id="pictureBig" width="100"/>

      <s:Button id="uploadLargePic" label="Browse.." click="uploadLargePic_clickHandler(event)"/>

      </mx:FormItem>

        • 1. Re: Event Bubbling Question...
          John Hall Level 4

          It's probably because the object that's actually dispatching the event is the button you're clicking on, so try something like the following:

           

                      protected function uploadLargePic_clickHandler( event:MouseEvent ):void {


                          if ( event.target.name == "uploadPic" ) {
                              Alert.show( "yaaa" );
                          }
                          if ( event.target.name == "uploadLargePic" ) {
                              Alert.show( "ya Again!!!" );
                          }
                      }
          1 person found this helpful
          • 2. Re: Event Bubbling Question...
            jerry98225 Level 1

            Thank you John. I am trying to do the task without using the id like "uploadPic" or "uploadLargePic". Is it possible to do it? Thanks again!

            • 3. Re: Event Bubbling Question...
              John Hall Level 4

              I imagine it is but you'll need to provide a little more detail re what you're trying to accomplish down the road. Are you going to have lots of buttons? If not, a switch/case statement. Would take care of most situations.

              1 person found this helpful
              • 4. Re: Event Bubbling Question...
                jerry98225 Level 1

                Thanks again John. I only have two buttons. I want to display different text on different textArea when the buttons are clicked. For example, when the uploadPic button is clicked, I want ID=picture textArea displays "This is the small picture". When the uploadBigPic button is clicked, the ID=pictureBig textarea displays "this is the big picture". By using the switch statement as you mention, I thought I still need to specify unloadPic or unloadLargePic id in the statement. Not sure  if I miss something.

                • 5. Re: Event Bubbling Question...
                  John Hall Level 4

                  Doing this stuff is always crash and burn and learn. so the answer would be different for almost everyone who was trying to solve the issue (maybe only in minor ways).  I assume you're going to upload them to a server somewhere, and if you REQUIRE that both be uploaded, then I guess I'd tend toward creating some sort of custom class that associates the small pic (thumbnail?) with the large pic and not allow submission until both are filled out. I'd still tend to use the id of the button with a switch/case to know which text field should receive the name of the file (maybe using the FileReference class?)  Is that kind of what you're doing? If so, you might have one object that is something like var thumbNail:FileReference and one with var imageFile:FileReference and they'd both be included in the custom class. Does that make any sense?

                  • 6. Re: Event Bubbling Question...
                    jerry98225 Level 1

                    I got what you said. Thank you for a very detailed explaination. Yes, I do want to upload two images to the server,and I want to display the file name when the user select their image file. I haven't worked on the uploading section yet but I only declare one instance of fileReference in the beginning and assign the different file name to that instance when the user click "uploadPic" and "uploadLargePic" button. As your guess (a very good one), I want to upload both thumbnail and big picture at the same time. This might create errors since fileReference only allow 1 file per upload. I haven't got that far yet, but I might use your custom class method eventually (Although I don't have too much experience on that. ).  Anyway, Thanks for the help.

                    • 7. Re: Event Bubbling Question...
                      John Hall Level 4

                      There are ways around it all, as you might guess. You can do the uploads sequentially, creating a new instance of the FileReference object in between or maybe you could use FileReferenceList to upload both, if they're going to the same directory.