7 Replies Latest reply on May 9, 2008 1:30 AM by kcell

    Call a function in Popup's parent??

    Handycam Level 1
      I have a popup window component called from one of my other components. It needs to call a function in the parent component.

      I tried:

      <mx:Form width="100%" id="saveForm">
      <mx:FormItem label="Name" width="100%" fontWeight="bold">
      <mx:TextInput id="saveName" width="100%" textAlign="left" fontWeight="normal" text="{fileName}"/>
      </mx:FormItem>
      <mx:HBox id="buttons" width="100%">
      <mx:Spacer width="100%"/>
      <mx:Button id="cancelButton" label="Cancel" styleName="normalButton" click="PopUpManager.removePopUp(this)" />
      <mx:Button id="saveButton" label="Save" styleName="normalButton" click="parent.saveRecipe()" />
      </mx:HBox>
      </mx:Form>

      But I get an error:
      1061: Call to a possibly undefined method saveRecipe through a reference with static type flash.display:DisplayObjectContainer.

      So it's not seeing it. How do I target the parent?
        • 1. Re: Call a function in Popup's parent??
          kcell Level 2
          Hi Handycam,

          we did it by the following way (may not be the only one)

          Make a custom Dialog AS class with events:
          [Event(name="createDocument", type="MYCreateDocumentEvent")]

          public class MyNewDocumentDialog extends TitleWindow
          {
          ..
          private function dispatchOKEvent():void{
          aEvent = new MYCreateDocumentEvent("createDocument", true);
          dispatchEvent( aEvent );
          }
          }

          Now when you create your PopUpWindow you can add a event listener for the event, which is than called.

          AS Code can be like:
          {
          ...
          var pop2:MyNewDocumentDialog= MyNewDocumentDialog(
          PopUpManager.createPopUp(this, MyNewDocumentDialog, true));
          ...
          pop2.addEventListener("createDocument", createDocumentCalled);
          PopUpManager.centerPopUp(pop2);
          }

          private function createDocumentCalled(event:MYCreateDocumentEvent):void{
          // Do something with the event
          }

          Hope it help you.
          If you find another solution I would be glad if you share it ;)

          best regards
          kcell
          • 2. Re: Call a function in Popup's parent??
            Handycam Level 1
            Thanks, but I guess I don't know how to create custom events, since your example gave me a lot of errors.

            Also, I created the AS file SaveDialogClass.as and an mxml SaveDialog.mxml with the type of <custom:SaveDialogClass> but although I was able to pop up the window, the mxml layout info was missing (the title window was empty).

            I was also unable to target elements in my window such as the text field (sWin.myTextField.text="foo"), Flex could not find the mxml elements. I realize I must be doing this wrong, but I am hazy on how to create MXML components and AS classes together (I usually just use "straight" mxml components).

            • 3. Re: Call a function in Popup's parent??
              kcell Level 2
              Hi Handycam,

              yes I assumed that, cause the code I posted may show you just the idea and also just in actionscript.

              If I find the time tommorow I will try to make a easy example which use mxml.

              best regards,
              kcell
              • 4. Re: Call a function in Popup's parent??
                Handycam Level 1
                The part that gets me is
                [Event(name="createDocument", type="MYCreateDocumentEvent")]

                private function dispatchOKEvent():void{
                aEvent = new MYCreateDocumentEvent("createDocument", true);
                dispatchEvent( aEvent );

                I'm trying to do tis all in the mxml document for my dialog. I was using your advice and the help files, and came up with

                <mx:Metadata>
                [Event(name="saveRecipe", type="myCustomEvent")]
                </mx:Metadata>

                and then I try this in my script block:
                private function handleSaveClick(aEvent:Event):void {
                aEvent = new myCustomEvent("saveMe", true);
                dispatchEvent(aEvent);
                }

                But I get :
                1180: Call to a possibly undefined method myCustomEvent.

                If I can just grasp this, I think I can do it
                • 5. Re: Call a function in Popup's parent??
                  Handycam Level 1
                  Ok, I changed it around a bit:

                  In the popup window:

                  private function handleSaveClick(aEvent:Event):void {
                  trace(aEvent.type);
                  dispatchEvent(aEvent);
                  PopUpManager.removePopUp(this);
                  }

                  <mx:Button id="saveButton" label="Save" click="handleSaveClick(event)" />

                  In the main app:

                  [Bindable]
                  private var sWin:SaveDialog;

                  private function saveDialog():void{
                  sWin = com.cyor.SaveDialog(PopUpManager.createPopUp(this,com.cyor.SaveDialog,true));
                  sWin.fileName = app._stepData0.@title;
                  sWin.saveName.setFocus();
                  sWin.addEventListener("click", myClickHandler);
                  }

                  private function myClickHandler(e:Event):void{
                  saveRecipe();
                  }

                  The main function is that "saveRecipe()". This works, but for some reason, it's called TWICE. Any ideas?
                  • 6. Re: Call a function in Popup's parent??
                    Handycam Level 1
                    OK, I've got it, if it helps anyone. It's almost what kcell suggested:

                    In the popup window component:
                    <mx:Metadata>
                    [Event(name="foobar", type="flash.events.Event")]
                    </mx:Metadata>

                    In the script:
                    private function handleSaveClick(aEvent:Event):void {
                    fileName=saveName.text;
                    dispatchEvent(new Event("foobar", true));
                    PopUpManager.removePopUp(this);
                    }

                    Then the button:
                    <mx:Button id="saveButton" label="Save" styleName="normalButton" click="handleSaveClick(event)" />

                    Then in the parent app:

                    private var sWin:SaveDialog;

                    private function saveDialog():void{
                    sWin = com.taunton.cyor.SaveDialog(PopUpManager.createPopUp(this,com.taunton.cyor.SaveDialog,tru e));
                    sWin.fileName = app._stepData0.@title;
                    sWin.saveName.setFocus();
                    sWin.addEventListener("foobar", myClickHandler);
                    }

                    private function myClickHandler(e:Event):void{
                    saveRecipe();
                    }
                    • 7. Re: Call a function in Popup's parent??
                      kcell Level 2
                      Hi Handycam,

                      great, you got it, thanks for sharing the code.

                      The "custom" event from my "example" was a new as class with extends the event class. The reason was that I wanted to send some more infos with the event to the parent app.

                      best regards,
                      kcell