将数据从弹出窗口发送到主应用程序。

时间:2011-08-02 11:48:07

标签: actionscript-3 popup custom-component custom-event

我没有得到我正在寻找的答案。 我想将请求数据发送到主应用程序。

    <?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml"  remove="titlewindow1_removeHandler(event)"
                width="400" height="220" layout="absolute" title="USER LOGIN">
    <mx:Metadata>
        [Event(name="commEvent", type="flash.events.Event")]
    </mx:Metadata>
    <mx:Script>
        <![CDATA[
            import data.Data;

            import mx.events.FlexEvent;
            import mx.managers.PopUpManager;


            [Bindable]
            public var userID:String;

            private function loginUser():void{
                trace("btn");
                var req:URLRequest = new URLRequest('http://localhost/CCN/userProcess.php');
                var loader:URLLoader = new URLLoader();
                req.method="POST";

                var variables:URLVariables = new URLVariables();
                variables.email= username.text;
                variables.password= password.text;
                variables.action= "login_user";
                req.data=variables;

                loader.addEventListener(Event.COMPLETE,onDataLoaded);
                loader.load(req);
            }
            protected function loginButton_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub

                loginUser();
            }


            private function onDataLoaded(e:Event):void{
                var xml:XML= new XML(e.target.data);
                if(xml.status=="success"){

                //SEND DATA TO MAIN APPLICATION ????
                    PopUpManager.removePopUp(this);

                }else{
                    fail.visible=true;
                    username.text="";
                    password.text="";
                    username.setFocus();
                }


            }

            protected function loginButton_keyDownHandler(ee:KeyboardEvent):void
            {
                // TODO Auto-generated method stub
                if(ee.keyCode==13){
                    loginUser();
                }
            }

            protected function titlewindow1_removeHandler(event:FlexEvent):void
            {
                // TODO Auto-generated method stub

            }

        ]]>
    </mx:Script>

    <mx:TextInput id="username" x="141" y="31" width="199" text=""/>
    <mx:TextInput keyDown="loginButton_keyDownHandler(event)" text="000" id="" x="141" y="84" width="199" displayAsPassword="true"/>
    <mx:Button id="loginButton" x="275" y="133" label="LOGIN" click="loginButton_clickHandler(event)"/>
    <mx:Label x="22" y="33" text="Email"/>
    <mx:Label x="22" y="86" text="Password"/>
    <mx:Label x="22" visible="false" y="135" id="fail" color="#FF0000" text="LOGIN FAILED"/>
</mx:TitleWindow>

这是主要的应用程序代码

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                minWidth="955" minHeight="600" backgroundColor="#FFFFFF"
                creationComplete="application1_creationCompleteHandler(event)" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.containers.TitleWindow;
            import mx.core.IFlexDisplayObject;
            import mx.events.CloseEvent;
            import mx.events.FlexEvent;
            import mx.managers.PopUpManager;

            //private var loginWindow:TitleWindow;
            public var user:String;
            private var login:Login

            private function application1_creationCompleteHandler(event:FlexEvent):void
            {
                // TODO Auto-generated method stub
                login = Login(
                    PopUpManager.createPopUp(this, Login, true));
                PopUpManager.centerPopUp(login);


                //login['loginButton'].addEventListener(MouseEvent.CLICK,onClose);
                login.addEventListener(CloseEvent.CLOSE,oncc)

            }
            private function onClose(e:Event):void{
                trace("Trace : "+login.userID);
            }
            private function


        ]]>
    </mx:Script>
</mx:Application>

1 个答案:

答案 0 :(得分:0)

我建议您通过添加自定义事件来解决此问题,因为您的标记暗示您可能会理解。

如果没有,请遵循以下步骤。

1)创建一个新的Event类型(在actionscript中扩展Event类 - 一定要覆盖clone())

2)在弹出窗口

上的父应用程序中为新的事件类型添加一个事件监听器

3)使弹出窗口在关闭之前调度新的事件类型

4)在事件处理程序中处理你正在寻找的任何内容(userID?)。

我建议将userID附加到实际事件,以便父级不直接寻址login.userID。从松耦合的角度来看,它更正确。也就是说,如果您不希望,可以通过不附加userID来简化解决方案。松耦合是一个伟大的目标,但如果你只打算一次使用这种关系,那就不是非常必要了。

如果您选择更紧密的耦合路线,那么您只需要使用自定义“类型”而不是扩展事件来发送事件。

如果你需要一个较低级别的例子(较少的描述,更多的代码)让我知道,我也可以提供帮助。

下面提供的示例是稍微复杂的版本,您可以在其中扩展事件以包含数据。

事件类::

package mycomponents
{
    import flash.events.Event;
    public class CustomEvent extends Event
    {
        public static const EVENT_TYPE_NAME:String = "myEventType"

        public var mUserID:String = "";
        public var mSuccess:Boolean = false;

        public function CustomEvent(aType:String, aUserID:String, aSuccess:Boolean)
        {
            super(aType)
            mUserID = aUserID;
            mSuccess = aSuccess;
        }

        override public function clone():Event
        {
            var lEvent:CustomEvent = new CustomEvent(mUserID, mSuccess);
            return lEvent;
        }
    }
}

在Popup ::

private var loginSuccessful:Boolean = false;

private function onDataLoaded(e:Event):void{
    var xml:XML= new XML(e.target.data);
    if(xml.status=="success"){

        userID = username.text;
        loginSuccessful = true;             
        //SEND DATA TO MAIN APPLICATION
        dispatchEvent(new CustomEvent(CustomEvent.EVENT_TYPE_NAME, userID, loginSuccessful );
        PopUpManager.removePopUp(this);

    }else{
        fail.visible=true;
        username.text="";
        password.text="";
        username.setFocus();
    }           
}

protected function titlewindow1_removeHandler(event:FlexEvent):void
{          
    if (!loginSuccessful)
        dispatchEvent(new CustomEvent(CustomEvent.EVENT_TYPE_NAME," userID, loginSuccessful ));
} 

在Main Application ::

import mycomponents.CustomEvent;

private function application1_creationCompleteHandler(event:FlexEvent):void 
{
    //...your code
    login.addEventListener(CustomEvent.EVENT_TYPE_NAME, handleLoginEvent);
}

private function handleLoginEvent(aEvent:CustomEvent)
{
    //My example code dispatches an event with mSuccess = false if the login prompt closes
    //without a successful login
    //
    //now do whatever you want with the information from the popup
    //aEvent.mUserID gets you ID
    //aEvent.mSuccess gets you success

}

在工作休息期间一起抛弃,所以没有承诺会按原样编译。