0

Basically I want to check if a user exists in a database using AMF (and that works great!). But then I want to return the boolean value to another function (in another class) that originally called the "checkUserExistance" function. But, since the database connection isn't immidiate, this function will always return a false value (even if "result" is true). So I would like to have the return-line inside the "onUserChecked"-function but that of course gives me an error. I thought I could create an eventListener, but then, the "return userExists"-line would also have to be inside another function, which doesnät work(?)... What can I do?

public function checkUserExistance(username:String) {
    var responderBOOLEAN:Responder = new Responder(onUserChecked, onFault)
    var userExists:Boolean = false;
    connection.connect(gateway);
    connection.call("User.checkUser", responderBOOLEAN, username);
    connection.close();
    function onUserChecked(result:Boolean):void {
        userExists = result;
    }
    return userExists;
}
MartinMo
  • 17
  • 4

2 Answers2

1

I'm sorry but you are trying to force an Asynchronous call to a Synchronous one and this is WRONG.

See here

You should learn how to handle events in the correct way.

What can i suggest you that helped me a lot is this

Community
  • 1
  • 1
SharpEdge
  • 1,782
  • 1
  • 10
  • 20
  • Yes, I'm aware of the problem (as I kind of stated in my text). But that is not really my question. Even if I add an event handler, that handler directs you to a function , right? (I'm not so good at the terminology here ;-) ) --> And then I'm having the same problem, since the "return userExists" connot be in another function. I know this must be really easy to fix, but I cannot find anything on it. Been searching for oven an hour now. – MartinMo Jun 05 '14 at 15:29
0

The only true answer here is to save userExists as a member variable, and dispatch event when the server returns you a response. The client side of the things should be similar to:

// add listener, ServerEvent is a custom event (see below)
server.addEventListener(ServerEvent.CHECK_RESPONSE, onCheckResponse);
server.checkUserExistance('username'); // start the query

function onCheckResponse(e:ServerEvent):void {
    if (e.userExists) {
    }
}


// inside server class
function onUserChecked(result:Boolean):void {
   userExists = true;
   dispatchEvent(new ServerEvent(ServerEvent.CHECK_RESPONSE, userExists));
}

/* ServerEvent is a custom class that extens Event
   Such classes are used so you can pass special properties in them
   via constructor (pass data, store it into member variable)
   and through getter for that variable.

   If you don't like it, simply add/dispatch Event.COMPLETE
   and use public property to get userExists from server
*/
Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
  • Graet! Figured it out eventually with some help of this answer. But used most of the code from a former question I posted myself. Haha. =) http://stackoverflow.com/questions/23481117/best-way-to-get-a-variable-from-another-class-in-as3 I think the biggest problem was that I already had written the if-statement in the other class really stupidly. It all messed up my head. Now it works though, thanks for putting me back on track. – MartinMo Jun 05 '14 at 17:05
  • No worries, I'm glad that helped. Keep up with the good design patterns and it will all end up well ;) – Andrey Popov Jun 05 '14 at 17:51