12 Replies Latest reply on Sep 1, 2009 7:47 AM by Andrew Rosewarn

    how to wait for an event till it happens like i have to wait till the button click

    nagasreekrishna

      Hi

       

      I have a function named say (Display() ) which returns a boolean value..This function creates a panel with 2 buttons added to it. My job is to set the boolen value to  true if "Ok" button is pressed and to false if "cancel" button is pressed.so i have to wait ..till one of the buttons is  clicked and  then return the boolean value from Display function..please tell me how to do that asap

       

      Thanks

        • 1. Re: how to wait for an event till it happens like i have to wait till the button click
          Phoenix_Guru Level 1

          it should go something like this:

           

          public function display():Boolean

          {

               if(buttonPressed = "OK")

               {

                    return true;

               }

               else

               {

                    display();

               }

          }

           

           

          I am not sure that it is the exact solution but the solution would be something similar.

          • 2. Re: how to wait for an event till it happens like i have to wait till the button click
            Andrew Rosewarn Level 3

            Hi there you could try it this way

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
                <mx:Script>
                    <![CDATA[
                       
                        private var displayValue:Boolean;  // will want oto set default true or false here
                       
                        private function setDisplayValue(event:MouseEvent):void {
                            if (event.currentTarget.label == 'ok') {
                                displayValue = true;
                            } else {
                                displayValue = false;
                            }
                        }
                       
                        public function display():Boolean {
                           
                            return displayValue;
                        }
                       
                       
                    ]]>
                </mx:Script>
                <mx:Panel x="176" y="186" width="250" height="200" layout="absolute">
                    <mx:Button x="30" y="45" label="ok" width="59" click="setDisplayValue(event)"/>
                    <mx:Button x="97" y="45" label="Cancel" click="setDisplayValue(event)"/>
                </mx:Panel>
                <mx:Button x="176" y="421" label="Button" click="display()"/>
            </mx:Application>

             

            Hope it helps

             

            Andrew

            • 3. Re: how to wait for an event till it happens like i have to wait till the button click
              _Natasha_ Level 4

              Hi,

              Panel can throw CloseEvent. And it's a usual thing to use this event in your situation.

              In this event there is a property detail.

              You can throw this event with positive detail if Ok, and negtive if Cancel.

              • 4. Re: how to wait for an event till it happens like i have to wait till the button click
                Barna Biro Level 3

                I for one don't see why this workaround would be useful... I mean, what the point in setting a boolean value depending on which button has been pressed? I would seem more obvious to "do action A if 'OK' has been pressed" or "do action B if "CANCEL" has been pressed" directly by assigning a listener to the two buttons ( each handler calling/doing what you want on click ).

                 

                I really don't see a point in setting a boolean value and deciding later if "OK" or "CANCEL" were clicked. This should be implement somehow into the component's behavior so it will "react naturally" instead of having to check trough N conditions to see if "OK" or "CANCEL" or who knows what else have been pressed with the help of a boolean variable.

                 

                Just my 2 cents,

                Barna Biro

                 

                EDIT: Or if this has to do with something else than opening/closing panels or reacting in whatever way after a buttons has been clicked then just ignore what I have said. If you are using this boolean variable to maybe set other properties that really can't be set other way, then yeah, do that Natasha or Andrew suggested. Since I don't have a clear understanding about what you are trying to do, it might be possible that you really don't have other solutions at hand than setting a flag for later use.

                • 5. Re: how to wait for an event till it happens like i have to wait till the button click
                  Andrew Rosewarn Level 3

                  Hey there

                   

                  I thought you might pop up .  I agree with the actions responding directly to the clicks.  I may of mistunderstood this myself.  I assumed something like (when he said creating a panel) there was a panel comp that had an ok button and a cancel and he needed to know which had been set.  Therefore I was going for the approach of being able to set this boolean, and then just retrieve this value with a public display function to return it whenever it was needed.

                   

                  However like you say a bit unclear.

                   

                  Andrew

                  • 6. Re: how to wait for an event till it happens like i have to wait till the button click
                    nagasreekrishna Level 1

                    public var rt:Boolean;

                    public function display():Boolean{
                               
                               
                                var b1:Button = new Button();
                                 var b2:Button = new Button();

                     

                                    b1.label = "OK";
                                    b1.addEventListener(MouseEvent.CLICK, closePopUp);
                                   
                                    b2.label = "Cancel";
                                    b2.addEventListener(MouseEvent.CLICK, closePopUpCancel);
                                    panel = new Panel();
                                    panel.title = "My Shortcomings";
                                    panel.width = 298;
                                    panel.height = 264;
                                    panel.addChild(b1);
                                    panel.addChild(b2);
                                PopUpManager.addPopUp(panel,this);
                                PopUpManager.centerPopUp(panel); 
                                
                             
                                return rt;
                      }

                     

                    public function closePopup():void{

                        rt=true;

                    }

                    public function closePopUpCancel():void{

                       rt=false;

                    }

                     

                    Hi all this is the code i wrote...iam calling the display function..from another function..but the display function is always returning false..becoz..its defaultly  intialized to false....i want the display function to return rt value as per the user click..if user clicks OK true must be returned,,else false must be returned..Can you suggest me how to do now

                    • 7. Re: how to wait for an event till it happens like i have to wait till the button click
                      Andrew Rosewarn Level 3

                      You could add event listeners to the component when you instansiate it, dispatch the events on the button click and then call close panel after the events have gone.

                       


                      This function doesn;t make sense to me as your trying to create the buttons etc in the function, add the event listeners and then close the panel all in one.  The functions not going to return anything.

                      • 8. Re: how to wait for an event till it happens like i have to wait till the button click
                        Barna Biro Level 3

                        @ Andrew: Ohh, ok, I understood something totally different.

                         

                        @ nagasreekrishna: So, if I now understand right, and you want to do what Andrew just said then you're not really doing it too right. Instead, simply dispatch a custom event to signal the option. Something like:

                         

                        // ...
                        
                        private var _changeEvent:Event;
                        
                        public function closePopup():void
                        {
                             _changeEvent = new Event("onOk");
                             this.dispatchEvent(_changeEvent);
                        }
                        
                        // same for the close just that then, you'd dispatch a new
                        // event of type "onClose": new Event("onCancel");
                        
                        // ...
                        

                         

                        This would be the more "simple solution". In case you are planning on also sending some data with your events, then the default Event won't be enough. You'll need to create a custom event that can store your additional data and pass it along.

                         

                        In your main application ( or component that is holding this component that is dispatching events ) you would subscribe to both "onOk" and "onCancel" events and react accordingly ( or you could only subscribe to the event that you are interested in ). Obviously, you could complicate things even more and even try getting a more clearer structure but that's a different tale.

                         

                        So, there's no point in setting a flag that would be read from the parent component ( who knows when ). Instead, simply dispatch custom events to signal change ( each action dispatching a custom type event so that you can react to that certain action and not to a generic action where you would be forced to have and check N condition in order to call the methods that you want ).

                         

                        Hope it helps,

                        Barna Biro

                        • 9. Re: how to wait for an event till it happens like i have to wait till the button click
                          Andrew Rosewarn Level 3

                          So for my example

                           

                          Main App

                           

                          <?xml version="1.0" encoding="utf-8"?>
                          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
                              <mx:Script>
                                  <![CDATA[
                                      import mx.managers.PopUpManager;
                                     
                                      private var myPanel:MyPanel = new MyPanel()
                                     
                                      private function init():void {
                                          myPanel.addEventListener('optionClicked', handleOption);
                                          PopUpManager.addPopUp(myPanel,this);   
                                      }
                                     
                                      private function handleOption(event:CustEvent):void {
                                          if (event.option) {
                                              // do true stuff
                                          } else {
                                              // do cancel stuff
                                          }
                                          PopUpManager.removePopUp(myPanel);
                                      }
                                  ]]>
                              </mx:Script>
                          </mx:Application>

                           

                           

                          MyPanel.mxml

                           

                          <?xml version="1.0" encoding="utf-8"?>
                          <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" layout="absolute">
                              <mx:Script>
                                  <![CDATA[
                                      private function handleClick(event:MouseEvent):void {
                                          var option:Boolean;
                                         
                                          if (event.currentTarget.label == 'OK') {
                                              option = true;
                                          }
                                         
                                          dispatchEvent(new CustEvent('optionClicked',option));
                                      }
                                     
                                  ]]>
                              </mx:Script>
                             
                              <mx:Button label="OK" click="handleClick(event)" x="68" y="63"/>
                              <mx:Button label="Cancel" click="handleClick(event)" x="135" y="63"/>
                             
                          </mx:Panel>

                           

                          CustEvent

                           

                          package
                          {
                              import flash.events.Event;

                           

                              public class CustEvent extends Event
                              {
                                  public var option:Boolean;
                                 
                                  public function CustEvent(type:String, option:Boolean, bubbles:Boolean=false, cancelable:Boolean=false)
                                  {
                                      super(type, bubbles, cancelable);
                                      this.option = option;
                                  }
                                 
                              }
                          }

                           

                          Any luck yet?

                           

                          Andrew

                          • 10. Re: how to wait for an event till it happens like i have to wait till the button click
                            Andrew Rosewarn Level 3

                            Oh and as Barna said you'd be better off sending an event for each click.  I just extended my other example to answer the true or false only in one custom event.  This isn;t very extensible if your gonna have more possibilities.

                             

                            Andrew

                            • 11. Re: how to wait for an event till it happens like i have to wait till the button click
                              Barna Biro Level 3

                              @ Andrew: There's one major problem with the approach you posted. You have a generic click handler for the buttons ( in both the custom component and main application ), so just imagine how the whole thing would look like with 10+ buttons or even more. Tons and tons of conditions whit maybe even more options to store and so on ( it's not a to pretty view ).

                               

                              It would be much better to dispatch specific events ( and also store data that has something to do with the event dispatched ) so that the corresponding handler ( in the main application, parent or wherever ) can react accordingly and execute all desired actions that come with that event ( also store the passed data, or decide if certain methods should be called depending on the data received from the event ). Storing properties in custom events is good, just that it's not really cool if you store a property so that later on the main application can somehow figure out if "ok, was this an OK or CANCEL? or was it a CLOSE?" and only after that see if "ok, do I have a condition that matches the sent property... hmm".

                               

                              I hope you understand my points.

                              • 12. Re: how to wait for an event till it happens like i have to wait till the button click
                                Andrew Rosewarn Level 3

                                Yep as I said in my PS I totally agree that your gonna end up with huge switch statements in both functions, and therefore agree with what you said.  I was being a bit lazy in just extending my example.  Shame on me .

                                 

                                Just for you Barna.

                                 

                                Main App

                                 

                                <?xml version="1.0" encoding="utf-8"?>
                                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
                                    <mx:Script>
                                        <![CDATA[
                                            import mx.managers.PopUpManager;
                                           
                                            private var myPanel:MyPanel = new MyPanel()
                                           
                                            private function init():void {
                                                myPanel.addEventListener('clickedOK', handleOK);
                                                myPanel.addEventListener('clickedCancel', handleCancel);
                                                PopUpManager.addPopUp(myPanel,this);   
                                            }
                                           
                                            private function handleOK(event:CustEvent):void {
                                                // do true
                                            }
                                           
                                            private function handleCancel(event:CustEvent):void {
                                                // to cancel
                                            }
                                        ]]>
                                    </mx:Script>
                                </mx:Application>

                                 

                                MyPanel

                                 

                                <?xml version="1.0" encoding="utf-8"?>
                                <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300" layout="absolute">
                                    <mx:Script>
                                        <![CDATA[
                                            private function handleClickOk(event:MouseEvent):void {
                                                dispatchEvent(new CustEvent('clickedOK',true));
                                            }
                                           
                                            private function handleClickCancel(event:MouseEvent):void {
                                                dispatchEvent(new CustEvent('clickedCancel',false));
                                            }
                                           
                                        ]]>
                                    </mx:Script>
                                   
                                    <mx:Button label="OK" click="handleClickOk(event)" x="68" y="63"/>
                                    <mx:Button label="Cancel" click="handleClickCancel(event)" x="135" y="63"/>
                                   
                                </mx:Panel>

                                 

                                And in this example now the custom event is not requed as the listened for events react to the actions.

                                 

                                Andrew