1 Reply Latest reply on May 16, 2006 3:14 AM by blemmo

    Feeling really dumb..

    targetplanet
      I am trying to build a class that, using Flash remoting, will check a name it is sent against names in a database to see if it would be a duplicate. The part that I am having trouble with is how to return the results to a variable calling the class function.

      so here is the class so far

      import mx.remoting.*;
      import mx.rpc.*;
      class com.zr.CheckName {
      private var cName:String;
      private var myServ:Service;
      function CheckName() {
      var gwURL:String = " http://sugarmomma:8500/flashservices/gateway";
      var serv:String = "services.CheckName";
      myServ = new Service(gwURL,null,serv,null, null);
      }
      //
      public function nameCheck(cname:String, ctype:String):Void{
      var pc endingCall = myServ.name(cname,ctype);
      pc.responder = new RelayResponder(this, "cName_Result", "cName_Fault");

      }
      private function cName_result(re:ResultEvent):Boolean {
      if(re.result == 1){
      return true;
      }else{
      return false;
      }
      }
      private function cName_fault(fe:FaultEvent):Void{
      trace("check name fault");
      }
      //
      //
      private function setName(n:String):Void {
      this.cName = n;
      }
      private function getName():String {
      return this.cName;
      }
      }

      and the instantiation would be :
      var cName:CheckName = new CheckName();
      var responce = cName.nameCheck(fName_txt.text, "username");
      trace("name used "+responce);

      obiously this wont work, because I would need a return in the function being called. but since the result isn't return to that function, there is nothing to return. I tried setting the results to a var, and then returning that from the called function, but it doesn't wait for the data before returning. what I was think was using somekind of listener so when the resualtEvent occures, the function would send back the results. but I can'r figure out how to implement that.
      Thanks
      Russ
        • 1. Feeling really dumb..
          blemmo Level 1
          Remoting is an asynchronous operation, so you can't use return values this (synchronous) way.
          You have to place your code in the RelayResponder callbacks, which isn't very nice because it destroys the class separation:
          private function cName_result(re:ResultEvent) {
          if(re.result == 1){
          _root.output = "name not taken";
          }
          or something like that. This way, you end up with code dealing with your main movie inside the external class, which is bad for reusing.

          Or you could try to use the object.watch method(). This is something I just thought of, never tried, but it may work:
          have a 'response object' in the class, e.g.
          public var response bject = new Object();
          In the RelayResponder callbacks, change a property of this object:
          private function cName_result(re:ResultEvent) {
          if(re.result == 1){
          response.nametaken = true;
          }
          Then you could watch() this property for changes from the main movie:
          cName.response.watch("nametaken", watchResponse);
          and define the watchResponse function to do what you need:
          function watchResponse(prop, oldVal, newVal){
          if (newVal == true) {
          trace ("The name is taken.");
          }
          else {
          trace ("The name is not taken.");
          }
          }

          This way it's still separated, and the watcher method would act like a callback. In theory, that is... but I think this might work. Please tell if you tried it.

          There's also the possibility to raise events in the class that can be reacted on from the main movie. I didn't use this yet, so I can't say much about this. Check out EventDispatcher (?) in the Flash Help if you're interested in that.

          hth,
          blemmo