Send data from popup to main application.

和自甴很熟 提交于 2019-12-13 05:49:07

问题


I'm not getting the answer I'm looking for. I want to send the request data i get to the main application.

    <?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>

here is the main application code

<?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:


I would recommend solving this by adding a custom event, as your tag implies you may understand.

If not, here are the steps you would follow.

1) Create a new Event type (extend the Event class in actionscript - be sure to override clone())

2) Add an event listener for your new Event type in the parent application on the popup

3) cause the popup to dispatch your new Event type before it closes

4) Handle whatever it is you're looking for (userID?) in the event handler.

I would recommend attaching the userID to the actual event, so that the parent is not directly addressing login.userID. From a loose coupling standpoint, it's more correct. That said, if you don't wish to, you can simplify the solution by NOT attaching the userID. Loose coupling is a great goal, but if you only plan to use this relationship once, it's not incredibly necessary.

If you choose to go the tighter coupling route, then you only have to dispatch an event with a custom "type" instead of an extended Event.

If you need a lower level example (less description, more code) let me know, and I can help with that as well.

The example provided below is the slightly more complex version, where you extend an event to contain the data.

Event class::

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;
        }
    }
}

In 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 ));
} 

And in the 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

}

Tossed that together in the middle of a break at work, so no promises will compile as-is.



来源:https://stackoverflow.com/questions/6911537/send-data-from-popup-to-main-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!