Search code examples
apache-flexflex4flex3flash-builder

Close title window from its child component flex


I am implementing a title window, which is calling from the main appication like this

protected function sampleButton_clickHandler(event:MouseEvent):void
            {
                var ttlWndw:SampleTitleWindow = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject, SampleTitleWindow, true) as SampleTitleWindow;
                PopUpManager.centerPopUp(ttlWndw);
            }

the title window will be this

<?xml version="1.0" encoding="utf-8"?>
<s: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:local="*"
               width="288" height="230">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <local:SampleVBox id="sampleVBox" x="108" y="83"/>
</s:TitleWindow>

in the title window i had another child component called sample vbox and this component looks like this

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[
            protected function closeBtn_clickHandler(event:MouseEvent):void
            {

            }
        ]]>
    </fx:Script>


    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Button id="closeBtn" label="Close" click="closeBtn_clickHandler(event)"/>
</mx:VBox>

My question is, I want to close the title window using the button in the sampleVBox component. please someone help me in this.

Thanks in advance!


Solution

  • One solution is to dispatch an event from the SampleVBox component that the parent TitleWindow will listen to. To do this, add some metadata to your SampleVBox class that indicates the class dispatches such an event:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx">
    
        <fx:Metadata> 
            [Event(name="close", type="flash.events.Event")] 
        </fx:Metadata> 
    
        <fx:Script>
            <![CDATA[
                protected function closeBtn_clickHandler(event:MouseEvent):void
                {
                    dispatchEvent(new Event(Event.CLOSE));
                }
            ]]>
        </fx:Script>
    
        <s:Button id="closeBtn" label="Close" click="closeBtn_clickHandler(event)"/>
    </mx:VBox>
    

    Now, that the SampleVBox declares that it dispatches a "close" event, you can listen for it in the TitleWindow:

    <s: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:local="*"
                   width="288" height="230">
    
        <fx:Script>
            <![CDATA[
                protected function onSampleBoxClose():void
                {
                    PopUpManager.removePopUp(this);
                }
            ]]>
        </fx:Script>
    
        <local:SampleVBox id="sampleVBox" x="108" y="83" close="onSampleBoxClose()"/>
    </s:TitleWindow>