3 Replies Latest reply on May 23, 2007 10:26 PM by scferda

    Static Method & Asynchronous Web Service... Save me

      In need of some Asynch ninjas to help me grok the way this works in Flex...

      Let's say we have an actionscript class called "Student." I'm trying to include a static method called findById(id:int) returning type Student. In theory, that lets me create a new Student by looking up the specific student via, say, a Web Services call...

      var newStudent:Student;
      newStudent = Student.findById(920);

      Now, the whole grand plan falls apart right when it's time to do the WebService call. I cannot for the life of me figure out how to get the result of the WebService call to be the return value of the findById() method. I've added the various listeners and created functions for them, but how do I get the resulting Object in the, say, webserviceHandler() method back into findById() to be returned?

      I am entirely sure I am just a moron about Asynch stuff. Great programming karma awaits the person who can make me slightly less dumb.

        • 1. Re: Static Method & Asynchronous Web Service... Save me
          Hi Steve,

          What you're describing is the way that synchronous calls work. You're not supposed to pull back the result into the method that fires off your asynchronous call.

          Looks like you're trying to turn an asynchronous thing into a synchronous thing. Instead you just need to adopt a slightly different approach.

          Declare your variable and set it to null. Make your call to Student.findById(x), but expect it to return void. Make the assignment to your variable in the result handler (the function listening for the result event), and then you're using an asynchronous paradigm.

          If you need more help you'll have to post the context of your particular problem (what call you're making, what handlers and variables are involved etc).

          • 2. Re: Static Method & Asynchronous Web Service... Save me
            You might want to change your Student.findById function to take two parameters: an integer ID and a callback function. The callback function has to take a ResultEvent parameter and from that you get your student.
            • 3. Re: Static Method & Asynchronous Web Service... Save me
              scferda Level 1
              Thank you both. Your suggestions helped me muddle my way into a solution. I knew I was approaching the problem in a synchronous way, but I didn't know where to go from there.

              The next problem is getting my Student object from the webservice to cast into a Student object within my Actionscript. But that's a subject for a different topic.

              Again, thank you.