10 Replies Latest reply on Jun 22, 2010 10:11 PM by BhaskerChari

    call functions in parent(?) from popup window(?)

    ltambs

      I'm not sure the terms parent or window in flex 4.

      I want to call functions in parent from parent window.

      it seems to be hard.

      Help me please.

       

      Thanks in advance.

       

      ========================== index.mxml ==========================
      (root element : <mx:Application> - xmlns:mx="http://www.adobe.com/2006/mxml")
      has Main.mxml in it

      ========================== Main.mxml ==========================
      (root element : <s:NavigatorContent> - xmlns:s="library://ns.adobe.com/flex/spark" )

      public function functInMain():void
      {
      //do something
      }
      protected function dataGrid_itemDoubleClickHandler(event:ListEvent):void
      {
      var arr:ArrayCollection = event.currentTarget.dataProvider;

      var titleWindowInstance:Popup =
        Popup(PopUpManager.createPopUp(this,
         Popup,
         true));    
      titleWindowInstance.rowData =  arr.getItemAt(event.rowIndex);
      PopUpManager.centerPopUp(titleWindowInstance); 
      }

      ========================== Popup.mxml ==========================

      protected function buttonClickedInpopup():void
      {

      Application.application.parentDocument.functInMain(null);//<<- here I want to correct

      }

        • 1. Re: call functions in parent(?) from popup window(?)
          Sangeeta Kalyani

          Hi Itambs,

           

          You can simply use the below code.

           

          protected function buttonClickedInpopup():void
          {

          parentApplication .functInMain();

          }

           

          Regards,

          Sangeeta Kalyani

          • 2. Re: call functions in parent(?) from popup window(?)
            ltambs Level 1

            this gives

            "ReferenceError: Error #1069: ~~~~" errro.

            • 3. Re: call functions in parent(?) from popup window(?)
              BhaskerChari Level 4

              Hi Itambs,

               

              Your functInMain function in your main application should be scoped to public and not private when you are using "Application.application.functInMain()".

               

              Check the scope visibility of the function once.

               

              If the problem still persists then please elaborate your questiob by posting some code.

               

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


              Thanks,

              Bhasker Chari

              • 4. Re: call functions in parent(?) from popup window(?)
                Sebastien V. Level 3

                The Application.application call should work if the method you'd like to call is public.

                 

                There is also another way of doing this: with a callback function.

                 

                In your pop up:

                 

                private var _callbackFunction:Function;
                
                public function set callbackFunction( value:Function ):void {
                     _callbackFunction = value;
                }
                
                protected function buttonClickedInpopup():void {
                     _callbackFunction(null);
                }
                

                 

                And in your Main.mxml:

                 

                var titleWindowInstance:Popup = Popup(PopUpManager.createPopUp(this, Popup, true));
                titleWindowInstance.rowData =  arr.getItemAt(event.rowIndex);
                titleWindowInstance.callbackFunction = functInMain;
                PopUpManager.centerPopUp(titleWindowInstance);  
                
                • 5. Re: call functions in parent(?) from popup window(?)
                  ltambs Level 1

                  Thanks.

                  Your callback function works ok

                   

                  why calling public functin won't work?

                   

                  here are neet examples.

                  index.mxml : front page

                  SearchEvents.mxml : inside index.mxml

                  UpdateEvent.mxml : poped up from SearchEvents.mxml

                   

                  ========index.mxml===========

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application pageTitle="CA Management"
                      xmlns:mx="http://www.adobe.com/2006/mxml"
                      xmlns:s="library://ns.adobe.com/flex/spark"
                      xmlns:i="test.*"
                      width="100%" height="100%" creationComplete="application1_creationCompleteHandler(event)" >
                  <mx:Script>
                    <![CDATA[
                     import mx.events.FlexEvent;

                     protected function application1_creationCompleteHandler(event:FlexEvent):void
                     {
                      myViewstack.selectedIndex = 0;
                     }

                    ]]>
                  </mx:Script>
                  <mx:ViewStack width="100%" height="100%" id="myViewstack">
                      <s:NavigatorContent id="EventManagement" label="Event Management"><i:SearchEvents x="0" y="0"/></s:NavigatorContent>
                  </mx:ViewStack>
                  </mx:Application>

                  ========SearchEvents.mxml=========

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:NavigatorContent xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                       xmlns:mx="library://ns.adobe.com/flex/mx" 
                       x="113" y="163"  
                       height="100%" width="750">
                  <fx:Script>
                    <![CDATA[
                    
                     import mx.collections.ArrayCollection;
                     import mx.controls.Alert;
                     import mx.events.FlexEvent;
                     import mx.events.ListEvent;
                     import mx.managers.PopUpManager;
                     import mx.rpc.events.ResultEvent;
                    
                    
                     public function parentFunction(param:String):void{
                      Alert.show("parent function called with " + param);
                     }
                    
                     protected function btnSearch_clickHandler(event:MouseEvent):void
                     {
                      var titleWindowInstance:UpdateEvent =
                       UpdateEvent(PopUpManager.createPopUp(this,
                        UpdateEvent,
                        true));    
                      PopUpManager.centerPopUp(titleWindowInstance);
                     }
                    
                    ]]>
                  </fx:Script>
                  <s:Button x="637" y="70" label="Popup" id="btnSearch" click="btnSearch_clickHandler(event)"/>
                  </s:NavigatorContent>
                  ========UpdateEvent.mxml=====

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
                      xmlns:s="library://ns.adobe.com/flex/spark"
                      xmlns:mx="library://ns.adobe.com/flex/mx"
                      xmlns:incl="incl.*"
                      showCloseButton="false"
                      title="Update Event" width="432" height="500"
                      xmlns:subscription="services.subscription.*"
                      xmlns:core="services.core.*"
                      horizontalAlign="left" xmlns:vo="vo.*"
                      >
                  <fx:Script>
                    <![CDATA[
                     import mx.core.Application;
                    
                     protected function parentFunctionCall(event:MouseEvent):void
                     {
                     
                      Application.application.parentFunction("param from popup");
                     }
                    
                    ]]>
                  </fx:Script>
                  <fx:Script>
                    <![CDATA[
                     import mx.collections.ArrayCollection;
                     import mx.controls.Alert;
                     import mx.controls.dataGridClasses.DataGridColumn;
                     import mx.core.Application;
                     import mx.events.FlexEvent;
                     import mx.formatters.*;
                     import mx.managers.PopUpManager;
                    
                     import popup.*;
                     [Bindable] public var rowData:Object ;//to get the parent row
                    ]]>
                  </fx:Script>
                  <s:Button label="parentFunctionCall" id="btnPar" click="parentFunctionCall(event)"/>
                  </mx:TitleWindow>

                  • 6. Re: call functions in parent(?) from popup window(?)
                    BhaskerChari Level 4

                    Hi Itambs,

                     

                    Your public parentFunction() should be in index.mxml file ...which is the main application file. But you have this function in SearchEvents.mxml but not in index.mxml.

                     

                    public function parentFunction(param:String):void{
                        Alert.show("parent function called with " + param);
                       }

                     

                    So you need to put this public function in index.mxml file which is your main application file.

                     

                    When you are trying to access Application.application it refers to main application file which is your index.mxml file but not SearchEvents.mxml ...since you dont have this function defined in your index.mxml file it could not call that function.

                     

                    Hope its clear for you now.

                     

                     

                    Thanks,

                    Bhasker Chari

                    • 7. Re: call functions in parent(?) from popup window(?)
                      pradeep reddy

                      Hi Bhasker chari,

                       


                            If i have function in other component instead of Main Application file... then how can we call that function from POPUP...? need help..

                       

                      Thanks,

                      Pradeep

                      • 8. Re: call functions in parent(?) from popup window(?)
                        ltambs Level 1

                        the function acts "refresh list" in SearchEvents.mxml.

                         

                        the index.mxml has lots of "*.mxml" which list data in grid.

                         

                        the SearchEvents.mxml is the right place I think.

                         

                        You mean there is no way to access functions in SerachEvents.mxml from the popup?
                        • 9. Re: call functions in parent(?) from popup window(?)
                          pradeep reddy Level 1

                          Hi Itambs,

                           

                          According to my knowledge you can call the function which are in other components. Just create a CUSTOMUIComponent which extends UIComponent and take instance of that CUSTOMUIComponent  and dispatch an event in popup using the instance.. and get the same instance of CUSTOMUIComponent  in the component in which the function is available and an eventListener to ther instance.. thats it.. the dispatched event will be captured in that component..!

                           

                           

                           

                          Thanks,

                          Pradeep

                          • 10. Re: call functions in parent(?) from popup window(?)
                            BhaskerChari Level 4

                            Hi Itambs,

                             

                            Yes ofcourse you cn access the function in SearchEvents.mxml file but you need to have a reference to that file in the popUp in which you are trying to access.

                             

                             

                            Thanks,

                            Bhasker Chari

                            1 person found this helpful