0 Replies Latest reply on Feb 4, 2008 7:19 AM by mapatel

    Simple data routing

    mapatel
      Hello guys,

      I am using amfphp with Flex and trying to get "Hello World!" from the server!
      And I do get the response from the server. But the problem I am facing while routing the server response to the other part of the application.

      I suggest you to go through the code for you to better understand whats going on. I have described the problem in detail below the code.

      Paginator.mxml (Main application):

      quote:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:component="components.*">
      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import classes.objects.User;

      private function login(data:User):void {
      mx.controls.Alert.show(data.hello);
      }
      ]]>
      </mx:Script>
      <component:login login="login(event.data)" horizontalCenter="0" verticalCenter="0"/>
      </mx:Application>


      components\login.as (Login component):
      quote:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Panel layout="vertical" title="Please Login" horizontalAlign="center" verticalAlign="middle" xmlns:mx=" http://www.adobe.com/2006/mxml">
      <mx:Metadata>
      [Event(name='login', type='classes.events.LoginEvent')]
      </mx:Metadata>

      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import classes.objects.User;
      import classes.events.LoginEvent;

      private function userLogin():void {
      var paginator:User = User.instance();

      paginator.username = username.text;
      paginator.password = password.text;

      paginator.sayHello();

      var event:LoginEvent = new LoginEvent('login', paginator);
      dispatchEvent(event);
      }

      private function clear():void {
      valUsername.enabled = false;
      valPassword.enabled = false;

      username.text = '';
      password.text = '';

      valUsername.enabled = true;
      valPassword.enabled = true;

      username.setFocus();
      }

      ]]>
      </mx:Script>
      <mx:StringValidator id="valUsername" source="{username}" property="text" minLength="2" maxLength="15" required="true"/>
      <mx:StringValidator id="valPassword" source="{password}" property="text" minLength="1" maxLength="25" required="true"/>
      <mx:Form>
      <mx:FormItem label="Username:">
      <mx:TextInput id="username" maxChars="15"/>
      </mx:FormItem>
      <mx:FormItem label="Password:">
      <mx:TextInput id="password" displayAsPassword="true" maxChars="25"/>
      </mx:FormItem>
      </mx:Form>
      <mx:ControlBar horizontalAlign="right" verticalAlign="middle">
      <mx:Button label="Login" click="userLogin()"/>
      <mx:Button label="Clear" click="clear()"/>
      </mx:ControlBar>
      </mx:Panel>


      classes\objects\User.as (User class):
      quote:

      package classes.objects{

      import classes.net.RemotingConnection;
      import flash.net.Responder;

      [Bindable]
      public class User{

      private static const _instance:User = new User(Lock);

      public var username:String;
      public var password:String;
      public var hello:String;

      private var gateway:RemotingConnection;

      public function User(lock:Class){
      if(lock != Lock){
      throw new Error('Invalid singleton access. Use User.instance');
      }
      }

      public static function instance():User {
      return _instance;
      }

      public function sayHello():void {
      _instance.loginStatus = '';
      gateway = new RemotingConnection(' http://www.domain.com/gateway.php');
      gateway.call('Users.login', new Responder(onResult, onFault));
      }

      private function onResult(result:String):void {
      _instance.hello= result;
      }

      private function onFault(fault:String):void {
      _instance.hello= fault;
      }
      }
      }

      internal class Lock{}


      classes\events\loginEvent.as (Login event):
      quote:

      package classes.events{

      import flash.events.Event;
      import classes.objects.User;

      public class LoginEvent extends Event{

      public var data:User;

      public function LoginEvent(type:String, data:User){
      super(type);
      this.data = data;
      }

      override public function clone():Event {
      return new LoginEvent(type, data);
      }
      }
      }


      classes\net\RemotingConnection (RemotingConnection for AMF):
      quote:

      package classes.net{

      import flash.net.NetConnection;
      import flash.net.ObjectEncoding;

      public class RemotingConnection extends NetConnection{

      public function RemotingConnection( sURL:String ){
      objectEncoding = ObjectEncoding.AMF3;
      if (sURL) connect( sURL );
      }

      public function AppendToGatewayUrl( s : String ) : void{
      //
      }
      }
      }


      OK, with the above code as it is, when I click on "Login" button of Login Component, I get blank alert box while I should or at least I want to have an alert box with the message "Hello World!".

      BUT, BUT, BUT if I do following minor change in User.as, I get a blank alert box for ONLY for the first time I click "Login" button. From then onwards, I do get an alert box with message "Hello World!":

      quote:

      public function sayHello():void {
      // _instance.loginStatus = ''; --> remove / comment out this line
      gateway = new RemotingConnection(' http://www.domain.com/gateway.php');
      gateway.call('Users.login', new Responder(onResult, onFault));
      }


      Any tips, hints, comments will be appreciated to solve this mystery!

      Thanks