开发者

Flex: Result event multiple times

开发者 https://www.devze.com 2022-12-29 07:25 出处:网络
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. 开发者_如何学运维T

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();
            }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消