tags:

views:

102

answers:

1

Hello everybody!!

I am trying to learn Flex and now i have the next code: http://pastebin.com/rZwxF7w1

This code is for my login component. I want to get a special string for encrypting my password. This string is given by my authservice. But when i login i get a multiple times a alert with Done(line 69 in the pastebin code or line 4 in the code on the bottom of this question). But i want that it shows one single time. Does someone know what is wrong with this code?

Tom

protected function tryLogin():void {
                encryptStringResult.addEventListener('result', function(event:ResultEvent):void {
                    var encryptString:String = event.result.toString();
                    Alert.show('Done');
                });
                encryptStringResult.token = auth.getEncryptString();
            }
+2  A: 

It's possible that tryLogin is called multiple times, meaning that you'd be adding multiple event handlers that does the same thing to the same event.

You could try the following:

protected function tryLogin():void {
            if (encryptStringResult.hasEventListener('result'))
                return;

            encryptStringResult.addEventListener('result', function(event:ResultEvent):void {
                encryptStringResult.removeEventListener('result', arguments.callee);
                var encryptString:String = event.result.toString();
                Alert.show('Done');
            });
            encryptStringResult.token = auth.getEncryptString();
        }

It will first check wether or not there already is an event listener for 'result' in which case it will simply return. Also, it will remove the (anonymous) event listener that gets added when the event has been dispatched.

macke
thank you! do you know how tryLogin is called multiple times? maybe it is bad code, or is it standard in flex? the while code is found here: http://pastebin.com/rZwxF7w1
Tom
Short of clicking the button twice, I can't see anything that should cause that behavior. Unless of course, Auth actually dispatches multiple times in which case you're going to have to supply us with that code as well to try and help you determine the cause of the issue.
macke
ok, thank you, i will look if i can solve it.
Tom