5 Replies Latest reply on Apr 14, 2008 11:13 PM by tekfetish

    Seeking advice for timer design

    tekfetish
      Hi,

      I am designing a timer application with Flex Builder 3 which is basically a stop watch that will remember when it was started if the page is reloaded (on a different computer, for instance). My original plan was to use JavaScript and mySQL but I'd like to give it a try with Flex first.

      Since Flex doesn't interact directly with databases I'm wondering if using mySQL to store the timer settings is the best idea? I think I could manage interfacing it with PHP but I'm not sure how much trouble this is going to be since I'm a new Flex user.

      How would you approach the project?

      Thanks,

      tekfetish
        • 1. Seeking advice for timer design
          justria Level 1
          Interfacing with a server part is very simple using Flex, PHP would be just fine - as well as any other server side language/system.

          Here is an Actionscript idea:

          You just create a HTTPService variable

          quote:

          /**
          * gateway : this is the communication layer with the server side php code
          */
          private var gateway:HTTPService = new HTTPService();


          Then set properties of this service

          quote:

          gateway.url = ' http://www.myServer.com/myScript.php';
          gateway.method = "POST";
          gateway.useProxy = false;


          If you'd like to pass parameters, use something like this:

          quote:

          parameters['someNameHere'] ='John Doe';
          parameters['whichTimer'] = 'timer 1';

          gateway.request = parameters;


          Then, define which functions should be called once the HTTPService returns [resultHandler on success and faultHandler on error:

          quote:

          gateway.addEventListener(ResultEvent.RESULT, resultHandler);
          gateway.addEventListener(FaultEvent.FAULT, faultHandler);


          Then do something in these functions:

          quote:

          public function resultHandler(e:ResultEvent):void
          {
          // your code here...
          }

          public function faultHandler(e:FaultEvent):void
          {
          var errorMessage:String = "Connection error: " + e.fault.faultString;
          if (e.fault.faultDetail)
          {
          errorMessage += "\n\nAdditional detail: " + e.fault.faultDetail;
          }
          Alert.show(errorMessage);
          }



          Finally, perform your request:

          quote:

          gateway.send();


          • 2. Seeking advice for timer design
            tekfetish Level 1
            Thanks justria,

            I was working on something similar in the Flex source using HTTPService. How do I go about interfacing the Flex application and the Actionscript?

            Thanks,

            tekfetish
            • 3. Re: Seeking advice for timer design
              ntsiii Level 3
              Great example code, Justria! I would add resultFormat="e4x", and in the handler:
              public function resultHandler(e:ResultEvent):void
              {
              var xmlResult:XML = XML(e.result);
              trace(xmlResult.toXMLString)
              }

              tekfetish, I am not sure I understand your question about "interfacing the Flex application and the Actionscript"

              AS code can reference ui components in scope if you have assigned an id in the mxml. UI elements can consume data stored in an AS model (like an xmlVariable) by direct assignment or by binding.

              Tracy
              • 4. Seeking advice for timer design
                tekfetish Level 1
                Tracy,

                By interfacing with Flex I was confused on exactly how to add the AS to the MXML. This is clear to me now.
                • 5. Re: Seeking advice for timer design
                  tekfetish Level 1
                  I've entered the code Justria provided. The MXML file looks like this: http://pastebin.com/m3ac7c30d

                  I am receiving the following error:

                  Error #1090: XML parser failure: element is malformed.

                  I am guessing this is because I am trying to download HTTP instead of XML... Is it possible to do what I am trying to do?

                  Otherwise I guess my PHP script will need to return XML in order to be read by Flex?

                  Thanks,

                  tekfetish