Skip to main content
April 11, 2011
Answered

A pesky callback/scope problem?

  • April 11, 2011
  • 1 reply
  • 909 views

Hello! I am having quite a time dealing with getting data passed around correctly and hope someone will be able to shed some light on the situation. Here is my setup... I have an AS3 project communicating with an FMSv4 which is in turn communicating with a remoting server. The mockup is like so - I am doing it from memory and not copy and paste so ignore any actual programming problems... it is the data scope issue I am trying to resolve 😐

// AS3

var rsp:Responder = new Responder(r_success, r_failure);

connection.call("fms_function", rsp);

function r_success(obj:Object)

{

     trace("it worked "+ obj);

}

function r_failure(obj:Object)

{

     trace("it failed");

}

// FMS - main.asc - netconnection etc has been correctly defined.

Client.prototype.fms_function = function()

{

     mygateway.call("remote_function", remoting_handler());

     function remoting_handler()

     {

          this.onResult(data)

          {trace("result received");

               return(data);          // ( 1 )

          }

          this.onStatus(data)

          {

               trace("whoops");

          }

     }

}

Okay, so that is the setup... as I said before please disregard possible errors in the syntax.. my code traces out correctly showing flow as expected and gets the data from the remoting server correctly. When the client calls the fms_function on the FMS it traces "it worked null", showing the data has not been returned through the method called in my main.asc. My problem is trying to get the data retrieved from the remoting server to return back through fms_function to the client function that called it, at point ( 1 ) above. I have nested it as above hoping the scope would resolve back to fms_function, however this fails. I have tried passing fms_function as a parameter into remoting_handler, but my attempts to pass remoting_handler(this) by reference, for example, failed. I have also tried not using a nested handler, but the data does not get returned through the fms_function method to the client. Using client.call and passing it back that way is not an option. How am I able to get the data from the remoting server to return to the client via fms_function? Any help would be appreciated. I have been trying for quite some time and have come up with a bunch of ways to not resolve my problem.

    This topic has been closed for replies.
    Correct answer SE_0208

    Before digging further - just wanted to ask why are you ruling out using Client.call() to return back the data? I am surely missing something so before digging more into this i thought i'll ask that info from you.

    1 reply

    SE_0208Correct answer
    Participating Frequently
    April 12, 2011

    Before digging further - just wanted to ask why are you ruling out using Client.call() to return back the data? I am surely missing something so before digging more into this i thought i'll ask that info from you.

    April 14, 2011

    That is a good question... the reason I am trying to avoid using Client.call() is because the client is doing a sequenced configuration routine comprised of nested calls to the fms. The success handler of each call will contain the next call item in the sequence and a failure or incorrect data will cancel the configuration sequence. Using Client.call() will force me to set up client-side methods for each step in the sequence and effectively destroy the nesting. Because of this, the best approach, I thought, was to get the FMS to just pass the data back through the client-side calling function... unless there is a better option?

    April 27, 2011

    SE_0208's reply compelled me to reconsider Client.call() and for that: Thank you. This is a non-trivial problem (at least as far as I have gone to investigate a way to do this) and chose to instead to just use what is now obviously a better method.