5 Replies Latest reply on Dec 4, 2009 10:32 AM by cputnam14

    How do I trigger a function in a custom pop up window everytime it opens?

    cputnam14

      Hello All,

      Here is an example of what my problem is. This is my main application.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

      <mx:Script>
          <![CDATA[
              import mx.managers.PopUpManager;
              public var custpop:MyPopUp = new MyPopUp();
         
              public function CallPopUp():void
              {
                  custpop.itemName = myDG.selectedItem.name;
                  custpop.itemCost = myDG.selectedItem.price;
                  PopUpManager.addPopUp(custpop,this,true);
                  PopUpManager.centerPopUp(custpop);
             
              }
          ]]>
      </mx:Script>


          <mx:XMLList id="Stuff">
              <item>
                  <name>Kitty</name>
                  <price>$5.00</price>
              </item>
              <item>
                  <name>Doggy</name>
                  <price>$4.00</price>
              </item>
              <item>
                  <name>Gold Fish</name>
                  <price>$3.00</price>
              </item>
              <item>
                  <name>Chicken</name>
                  <price>$2.00</price>
              </item>
          </mx:XMLList>


          <mx:DataGrid id="myDG" x="203" y="147" dataProvider="{Stuff}">
              <mx:columns>
                  <mx:DataGridColumn headerText="Item Name" dataField="name"/>
                  <mx:DataGridColumn headerText="Price" dataField="price"/>
                  <mx:DataGridColumn headerText="Action" dataField="price">
                      <mx:itemRenderer>
                                      <mx:Component>
                                      <mx:Button label="Click Me" click="outerDocument.CallPopUp();"/>
                                      </mx:Component>                           
                              </mx:itemRenderer>
                  </mx:DataGridColumn>
              </mx:columns>
          </mx:DataGrid>

      </mx:Application>

       

       

      Here is my custom pop up

      <?xml version="1.0" encoding="utf-8"?>
      <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
           width="400" height="300" title="Oh Hi" showCloseButton="true" close="PopUpManager.removePopUp(this);"
           initialize="showData();" creationComplete="showData();">
          <mx:Script>
              <![CDATA[
                  import mx.managers.PopUpManager;
                  import mx.controls.Alert;
                  [Bindable] public var itemName:String;
                  [Bindable] public var itemCost:String;
                 
                  private function showData():void
                  {
                      Alert.show("You selected " + itemName + ". It costs " + itemCost,"Hi");
                  }
                 
                 
              ]]>
          </mx:Script>
          <mx:Label x="31" y="64" text="Your selection is:"/>
          <mx:Label x="140" y="64" text="{itemName}"/>
          <mx:Label x="31" y="90" text="It costs:"/>
          <mx:Label x="140" y="90" text="{itemCost}"/>
         
         
         
         
         
      </mx:TitleWindow>

       

      It is pretty straight forward. I have a datagrid that when the user clicks the button a custom pop up window opens and displays info on what they clicked. I need to fire the "showData" function every time the pop up window opens, but for the life of me I can not figure out how to do that. I've tried creationComplete (only fires the first time), show (never fires), activate (only fires the first time, and fires before the data is passed in)

       

      Anyone have any ideas on what action I should use for this?

        • 1. Re: How do I trigger a function in a custom pop up window everytime it opens?
          danny_run Level 1

          In the function CallPopUp just throw an event custPop.dispatchEvent(new YourEvent()) and catch it in your customPopUp. You can throw for example a show event.

          1 person found this helpful
          • 2. Re: How do I trigger a function in a custom pop up window everytime it opens?
            Gregory Lafrance Level 6

            If this post answers your question or helps, please mark it as such.


            Greg Lafrance
            www.ChikaraDev.com

            Flex Development and Support Services

             

            This code solves you problem:

             

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
            width="400" height="300" title="Oh Hi" showCloseButton="true"
              close="PopUpManager.removePopUp(this);" creationComplete="showData();">
              <mx:Script>
                <![CDATA[
                  import mx.managers.PopUpManager;
                  import mx.controls.Alert;
                 
                  [Bindable] public var itemName:String;
                  [Bindable] public var itemCost:String;

                  private function showData():void{
                    Alert.show("You selected " + itemName + ". It costs " + itemCost,"Hi");
                  }
                ]]>
              </mx:Script>
              <mx:Label x="31" y="64" text="Your selection is:"/>
              <mx:Label x="140" y="64" text="{itemName}"/>
              <mx:Label x="31" y="90" text="It costs:"/>
              <mx:Label x="140" y="90" text="{itemCost}"/>
            </mx:TitleWindow>

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
            <mx:Script>
                <![CDATA[
                  import mx.events.CloseEvent;
                    import mx.managers.PopUpManager;
                    public var custpop:MyPopUp;
                
                    public function CallPopUp():void
                    {
                        custpop = new MyPopUp();
                        custpop.itemName = myDG.selectedItem.name;
                        custpop.itemCost = myDG.selectedItem.price;
                        PopUpManager.addPopUp(custpop,this,true);
                        PopUpManager.centerPopUp(custpop);
                        custpop.addEventListener(CloseEvent.CLOSE, closeFunc);
                    }
                    
                    private function closeFunc(evt:CloseEvent):void{
                      custpop = null;
                    }
                ]]>
            </mx:Script>
            
                <mx:XMLList id="Stuff">
                    <item>
                        <name>Kitty</name>
                        <price>$5.00</price>
                    </item>
                    <item>
                        <name>Doggy</name>
                        <price>$4.00</price>
                    </item>
                    <item>
                        <name>Gold Fish</name>
                        <price>$3.00</price>
                    </item>
                    <item>
                        <name>Chicken</name>
                        <price>$2.00</price>
                    </item>
                </mx:XMLList>
            
                <mx:DataGrid id="myDG" x="203" y="147" dataProvider="{Stuff}">
                    <mx:columns>
                        <mx:DataGridColumn headerText="Item Name" dataField="name"/>
                        <mx:DataGridColumn headerText="Price" dataField="price"/>
                        <mx:DataGridColumn headerText="Action" dataField="price">
                            <mx:itemRenderer>
                                            <mx:Component>
                                            <mx:Button label="Click Me" click="outerDocument.CallPopUp();"/>
                                            </mx:Component>                            
                                    </mx:itemRenderer>
                        </mx:DataGridColumn>
                    </mx:columns>
                </mx:DataGrid>
            </mx:Application>
            
            
            
            • 3. Re: How do I trigger a function in a custom pop up window everytime it opens?
              cputnam14 Level 1

              I was able to use addedToStage to get it to work as well as the method listed above.

               

              Would there be any advantage or disadvantage for using one method vs. the other?

              • 4. Re: How do I trigger a function in a custom pop up window everytime it opens?
                Gregory Lafrance Level 6

                Depends on your situation, but sometimes you want to reset a popup, so setting it to null on close is often good.

                 

                But you probably need to call removeEventListener to eliminate references to the popup:

                 

                 

                 

                private function closeFunc(evt:CloseEvent):void{

                    custpop.removeEventListener(CloseEvent.CLOSE, closeFunc);

                    custpop =

                null;

                }

                 

                Greg Lafrance
                www.ChikaraDev.com

                Flex Development and Support Services

                • 5. Re: How do I trigger a function in a custom pop up window everytime it opens?
                  cputnam14 Level 1

                  I am hoping that using your method will resolve some memory issues I am having.

                   

                  I assume that when you set it to null and remove the event listner the garbage collecter will free up whatever memory it was using. Does anyone know if that is a true statement?