flex4模拟从一个模块传递到另一个模块的数据

时间:2014-04-13 12:39:47

标签: actionscript-3 flex flex4

我有一个加载两个模块的Flex 4应用程序。第一个模块是用户登录模块,第二个模块是用户平衡。当显示用户应用程序启动登录屏幕时。当用户登录此模块时,传递给java并处理一些验证并返回到flex。在flex中,如果用户有效,则将该用户名传递给第二个模块。在该模块中,使用该名称再次将用户名传递给java并获得该用户的余额。

但我的代码无效?

正在使用此代码: -

modules.mxml

<?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" minWidth="955" minHeight="600"  xmlns:views="views.*">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<views:module2/>
<views:module1>

</views:module1>
</s:Application>

module1.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.rpc.events.ResultEvent;

        protected function login_resultHandler(event:ResultEvent):void
        {
            if(har.text=="valid"){
                var username:String = "module2.swf?" + "username=" +user.text;
                bordercontainerlogin.visible=false;

            }else if(har.text=="not-valid")
            {
                validation.text="! make sure user name & password correct";         
            }
            else
                validation.text=har.text
        }
        protected function user_focusInHandler(event:FocusEvent):void
        {
            if((user.text=="User name")||(user.text==""))
            {
                user.text="";
            }
        }

        protected function user_focusOutHandler(event:FocusEvent):void
        {
            if((user.text=="User name")||(user.text==""))
            {
                user.text="User name";
            }
        }
        protected function pass_focusInHandler(event:FocusEvent):void
        {
            pass.displayAsPassword=true;
            if((pass.text=="Password")||(pass.text==""))
            {
                pass.text="";
            }
        }
        protected function pass_focusOutHandler(event:FocusEvent):void
        {
            if((pass.text=="Password")||(pass.text==""))
            {
                pass.displayAsPassword=false;
                pass.text="Password";
            }

        }

        protected function button1_clickHandler(event:MouseEvent):void
        {
            if(((user.text=="User name")||(user.text=="")) || ((pass.text=="Password")||(pass.text=="")))

            {

                if((user.text=="User name")||(user.text==""))
                {
                    //validation.text="! Make sure user name shouidn't be empty";
                    Alert.show(user.text);                  }

                else if((pass.text=="Password")||(pass.text==""))
                {
                    validation.text="! Make sure Password shouidn't be empty";  
                }
            }
            else
            {
                login.cancel();
                login.send();
            }


        }
        protected function button1_mouseOutHandler(event:MouseEvent):void
        {
            Mouse.cursor=MouseCursor.ARROW;
        }

        protected function button1_mouseOverHandler(event:MouseEvent):void
        {
            Mouse.cursor=MouseCursor.BUTTON;    
        }
    ]]>
</fx:Script>
<fx:Declarations>
    <s:HTTPService id="login" url="http://192.168.1.4:8400/myapp/login" method="POST" result="login_resultHandler(event)">
        <s:request xmlns="">
            <myname>{user.text}</myname>
            <passwd>{pass.text}</passwd>
        </s:request>
        </s:HTTPService>
</fx:Declarations>
<s:BorderContainer x="0" y="-1" width="100%" height="100%" id="bordercontainerlogin" backgroundColor="#fa0000" backgroundAlpha=".2">
    <s:ModuleLoader id="modulerload"/>
    <s:Panel width="257" height="205" fontWeight="bold" horizontalCenter="-44" verticalCenter="-56" id="loginpanel" visible="true" title="User Login">


        <s:TextInput id="user" y="61"  horizontalCenter="-3" text="User name" focusIn="user_focusInHandler(event)" focusOut="user_focusOutHandler(event)"/>
        <s:TextInput id="pass" y="99" focusIn="pass_focusInHandler(event)"
                     focusOut="pass_focusOutHandler(event)" horizontalCenter="-3" text="Password"/>

        <s:Button y="137" label="LOGIN" click="button1_clickHandler(event)" horizontalCenter="-4"
                  mouseOut="button1_mouseOutHandler(event)"
                  mouseOver="button1_mouseOverHandler(event)"/>
        <s:Label id="validation" x="4" y="167" color="#D90D0D"/>

    </s:Panel>
    <!--<s:ComboBox id="resultData" dataProvider="{reg.lastResult.status}" visible="true" selectedIndex="0"/>-->
    <s:TextInput id="har" x="43" y="149" text="{login.lastResult.status}" visible="false" />
    </s:BorderContainer>
           </s:Module>

module2.mxml

<?xml version="1.0" encoding="utf-8"?>
   <s:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300" creationComplete="module1_creationCompleteHandler(event)">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        import mx.rpc.events.ResultEvent;

        var username:String;
        protected function getbalance_resultHandler(event:ResultEvent):void
        {
            username= this.loaderInfo.url.toString();
            getbalance.cancel();
            getbalance.send();

        }

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

        }

    ]]>
</fx:Script>
<fx:Declarations>
    <s:HTTPService id="getbalance" url="http://192.168.1.4:8400/myapp/getbalance" method="POST" result="getbalance_resultHandler(event)">
        <s:request xmlns="">
            <myname>{username}</myname>

        </s:request>
    </s:HTTPService>

</fx:Declarations>
<s:Label text="welcome {username}" id="welcomelabel" />
<s:TextInput text="{getbalance.lastResult.status}" id="balance"/>


   </s:Module>

1 个答案:

答案 0 :(得分:0)

因为module2在这个时候是creationComplete,所以,你可以因为module2在这个时候是creationComplete,所以,你可以在login_resultHandler加载module2时