从弹出发送数据到主应用程序。 [英] Send data from popup to main application.

查看:132
本文介绍了从弹出发送数据到主应用程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到我想要的答案。 我想送请求数据我到主应用程序。

 < XML版本=1.0编码=UTF-8&GT?;
< MX:titleWindow正在的xmlns:MX =htt​​p://www.adobe.com/2006/mxml删除=titlewindow1_removeHandler(事件)
                WIDTH =400HEIGHT =220布局=绝对称号=用户登录>
    < MX:元数据>
        [事件(名称=的CommEvent,键入=对象类型:flash.events.Event)
    < / MX:元数据>
    < MX:脚本>
        <![CDATA [
            进口数据。数据;

            进口mx.events.FlexEvent;
            进口mx.managers.PopUpManager;


            [可绑定]
            公共变种用户名:字符串;

            私有函数loginUser():无效{
                跟踪(BTN);
                VAR REQ:的URLRequest =新的URLRequest(HTTP://localhost/CCN/userProcess.php');
                VAR装载机:的URLLoader =新的URLLoader();
                req.method =POST;

                VAR变量:使用URLVariables =新的URLVariables();
                variables.email = username.text;
                variables.password = password.text;
                variables.action =login_user;
                req.data =变量;

                loader.addEventListener(引发Event.COMPLETE,onDataLoaded);
                loader.load(REQ);
            }
            保护功能loginButton_clickHandler(事件:MouseEvent)方法:无效
            {
                // TODO自动生成方法存根

                loginUser();
            }


            私有函数onDataLoaded(五:事件):无效{
                VAR的xml:XML =新的XML(e.target.data);
                如果(xml.status ==成功){

                //将数据发送到主应用程序????
                    PopUpManager.removePopUp(本);

                }其他{
                    fail.visible = TRUE;
                    username.text =;
                    password.text =;
                    username.setFocus();
                }


            }

            保护功能loginButton_keyDownHandler(EE:KeyboardEvent的):无效
            {
                // TODO自动生成方法存根
                如果(ee.key code == 13){
                    loginUser();
                }
            }

            保护功能titlewindow1_removeHandler(事件:FlexEvent):无效
            {
                // TODO自动生成方法存根

            }

        ]]≥
    < / MX:脚本>

    < MX:的TextInput ID =用户名X =141Y =31WIDTH =199文本=/>
    < MX:的TextInput的keyDown =loginButton_keyDownHandler(事件)文本=000ID =X =141Y =84WIDTH =199displayAsPassword =真/>
    < MX:按钮的ID =loginButtonX =275Y =133的标签=登录点击=loginButton_clickHandler(事件)/>
    < MX:标签X =22Y =33文本=电子邮件/>
    < MX:标签X =22Y =86文本=密码/>
    < MX:标签X =22可见=假Y =135​​ID =不及格的颜色=#FF0000文本=登录失败/>
< / MX:titleWindow正在>
 

下面是主要的应用程序code

 < XML版本=1.0编码=UTF-8&GT?;
< MX:应用的xmlns:MX =htt​​p://www.adobe.com/2006/mxml
                =了minWidth955了minHeight =600的backgroundColor =#FFFFFF
                的creationComplete =application1_creationCompleteHandler(事件)布局=绝对>
    < MX:脚本>
        <![CDATA [
            进口mx.containers.TitleWindow;
            进口mx.core.IFlexDisplayObject;
            进口mx.events.CloseEvent;
            进口mx.events.FlexEvent;
            进口mx.managers.PopUpManager;

            //私人VAR登录窗口:titleWindow正在;
            公共变种用户:字符串;
            私人VAR登录:登录

            私有函数application1_creationCompleteHandler(事件:FlexEvent):无效
            {
                // TODO自动生成方法存根
                登录=登录(
                    PopUpManager.createPopUp(这一点,登录,真));
                PopUpManager.centerPopUp(登录);


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

            }
            私有函数的OnClose(五:事件):无效{
                跟踪(跟踪+ login.userID);
            }
            私有函数


        ]]≥
    < / MX:脚本>
< / MX:用途>
 

解决方案

我会建议通过添加自定义事件解决这一点,因为你的标签意味着你可能明白了。

如果没有,这里的步骤,你将跟随。

1)创建一个新的事件类型(扩展事件类的动作 - 一定要重写的clone())

2)添加一个事件侦听器为您的新事件类型在弹出的

父应用程序

3)导致的弹出派遣新的事件类型关闭之前,

4)手柄不管它是你正在寻找(用户ID?)在事件处理程序。

我建议将所述用户标识的实际情况下,以使母体不直接寻址login.userID。从一个松耦合的角度来看,它更正确。这就是说,如果你不想,你可以简化解决方案,通过不安装用户ID。松耦合是一个伟大的目标,但如果你只打算使用这种关系一次,这不是令人难以置信的必要。

如果你选择去了更紧密的耦合途径,那么你只需要派遣事件定制的类型,而不是一个扩展的事件。

如果你需要一个较低的水平例子(少说明,更多的code)让我知道,我可以帮助这一点。

下面提供的例子是稍微复杂一点的版本,在那里你扩展的事件来存放数据。

事件类::

 包mycomponents
{
    进口对象类型:flash.events.Event;
    公共类自定义事件扩展事件
    {
        公共静态常量EVENT_TYPE_NAME:字符串=myEventType

        公共变种mUserID:字符串=;
        公共变种mSuccess:布尔= FALSE;

        公共功能自定义事件(ATYPE:字符串,aUserID:字符串,aSuccess:布尔)
        {
            超(ATYPE)
            mUserID = aUserID;
            mSuccess = aSuccess;
        }

        重写公共职能的clone():事件
        {
            VAR勒:自定义事件=新的自定义事件(mUserID,mSuccess);
            返回勒;
        }
    }
}
 

在弹出::

 私人变种loginSuccessful:布尔=假;

私有函数onDataLoaded(五:事件):无效{
    VAR的xml:XML =新的XML(e.target.data);
    如果(xml.status ==成功){

        用户ID = username.text;
        loginSuccessful = TRUE;
        //将数据发送到主应用程序
        则dispatchEvent(新的自定义事件(CustomEvent.EVENT_TYPE_NAME,用户ID,loginSuccessful);
        PopUpManager.removePopUp(本);

    }其他{
        fail.visible = TRUE;
        username.text =;
        password.text =;
        username.setFocus();
    }
}

保护功能titlewindow1_removeHandler(事件:FlexEvent):无效
{
    如果(!loginSuccessful)
        则dispatchEvent(新的自定义事件(CustomEvent.EVENT_TYPE_NAME,用户ID,loginSuccessful));
}
 

和在主应用程序::

 进口mycomponents.CustomEvent;

私有函数application1_creationCompleteHandler(事件:FlexEvent):无效
{
    //...your code
    login.addEventListener(CustomEvent.EVENT_TYPE_NAME,handleLoginEvent);
}

私有函数handleLoginEvent(aEvent:自定义事件)
{
    //我的例子code调度与mSuccess事件= false如果在登录提示关闭
    //没有登录成功
    //
    //现在做任何你想要从弹出的信息
    //aEvent.mUserID让你的ID
    //aEvent.mSuccess让你成功

}
 

扔一起在工作中间休息,所以没有承诺将编译原样。

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>

解决方案

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.

这篇关于从弹出发送数据到主应用程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆