开发者

Flex dispached event inside TitleWindow is not system wide?

I have a tree, which childs dispaching event, if i run the object outside TitleWindow - everything is working fine, but if i encapsulate it inside a TitleWindow as into the source below - the event is not anymore dispached system wide.

<?xml version="1.0" encoding="utf-8"?>
<s:Application 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:sparkTree="com.sparkTree.*"
               creationComplete="init()">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.managers.PopUpManager;

            private function init():void
            {
                PopUpManager.addP开发者_如何学CopUp( xWin, this );
                this.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );
            }

        ]]>
    </fx:Script>

    <s:TitleWindow id="xWin" >

        <s:HGroup width="100%" height="100%">
            <sparkTree:Tree labelField="label" 
                    dataProvider="{dataProviderXML}" 
                    width="300" height="500" 
                    textRollOverColor="yellow" 
                    textSelectedColor="0xFFFFFF"
                    itemRenderer="com.sparkTree.XItemRenderer">

                <sparkTree:layout>
                    <s:VerticalLayout gap="0" variableRowHeight="true"/>
                </sparkTree:layout>
            </sparkTree:Tree>
        </s:HGroup>
    </s:TitleWindow>
</s:Application>


Change your

this.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );

to

xWin.addEventListener( MyEvent.REQUEST_FORWARD, function( e : MyEvent ):void{ Alert.show( ":P" ) } );

Or maybe you're using event bubbling?

I don't know details about your code and see it very problematic but this code works fine:

<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init()" xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.managers.PopUpManager;

        protected function init():void
        {
            PopUpManager.addPopUp(xWin, this);
            systemManager.addEventListener(MyEvent.REQUEST_FORWARD, function(e:MyEvent):void
            {
                Alert.show(":P")
            });
        }
    ]]>
    </fx:Script>
    <fx:Declarations>
        <s:TitleWindow id="xWin">
            <s:Button click="xWin.dispatchEvent(new MyEvent(MyEvent.REQUEST_FORWARD))" horizontalCenter="0"
                verticalCenter="0" />
        </s:TitleWindow>
    </fx:Declarations>
</s:Application>

Where MyEvent is:

package
{
import flash.events.Event;

public class MyEvent extends Event
{
    public static const REQUEST_FORWARD:String = "requestForward";

    public function MyEvent(type:String)
    {
        super(type, true, false);
    }
}
}

But what about me personally I recommend to to change all your code :)

First of all I strongly recommend you to extract your window in a separate component (you should do it all the time and use inline components only for prototyping):

<?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">
    <fx:Metadata>
        [Event(name="requestForward", type="MyEvent")]
    </fx:Metadata>
    <s:Button click="dispatchEvent(new MyEvent(MyEvent.REQUEST_FORWARD))" horizontalCenter="0"
        verticalCenter="0" />
</s:TitleWindow>

Second, don't use bubbling events without need especially outside components:

package
{
import flash.events.Event;

public class MyEvent extends Event
{
    public static const REQUEST_FORWARD:String = "requestForward";

    public function MyEvent(type:String)
    {
        super(type);
    }
}
}

Third, try not to use inner functions and create separate methods. It is more readable and it is easier to unsubscribe. So our final application:

<?xml version="1.0" encoding="utf-8"?>
<s:Application creationComplete="init()" xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark">
    <fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.managers.PopUpManager;

        protected function init():void
        {
            var win:MyWindow = MyWindow(PopUpManager.createPopUp(this, MyWindow));
            win.addEventListener(MyEvent.REQUEST_FORWARD, win_requestForwardHandler);
        }

        private function win_requestForwardHandler(event:MyEvent):void
        {
            Alert.show(":P");
            var win:MyWindow = MyWindow(event.currentTarget);
            win.removeEventListener(MyEvent.REQUEST_FORWARD, win_requestForwardHandler);
        }
    ]]>
    </fx:Script>
</s:Application>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜