7 Replies Latest reply on Mar 16, 2007 11:25 AM by dimival

    AsyncToken vs Listeners

    dimival Level 1
      Hi, i have been having some issues when registering the result and fault events listeners for a DataService component, so i was thinking about replacing the listeners with async tokens but i have some doubts about this.

      I saw on the documentation an example that combines the two of them, so i was wondering if anyone could tell me what's the best practice for using async tokens? do i use them alone or do i use them combined with event listeners?

      Thanks
        • 1. Re: AsyncToken vs Listeners
          phi2265
          I'm trying to figure out how to change a value with DataService. Right now I'm using myDataService.createItem(myItem); but I don't want it to get a new ID, and the error I'm getting tells me there is already an item with ID of 1. Do you know how to get the Item, change it, then commit the changes? The documentation isn't very clear to me on this.

          I think I'm reading about the same stuff you are so I figured you might possibly know this.

          Thanks..

          • 2. Re: AsyncToken vs Listeners
            dishmael Level 1
            quote:

            Originally posted by: dimival
            Hi, i have been having some issues when registering the result and fault events listeners for a DataService component, so i was thinking about replacing the listeners with async tokens but i have some doubts about this.

            I saw on the documentation an example that combines the two of them, so i was wondering if anyone could tell me what's the best practice for using async tokens? do i use them alone or do i use them combined with event listeners?

            Thanks


            Use the tokens with event handlers:

            // Assume service is a property that points to your service

            private function init():void {
            service.addEventListener( ResultEvent.RESULT, resultHandler );
            }

            // Markers must be unique
            private function helloWorld():void {
            var call:Object = service.sayHello();
            call.marker = "helloWorld";
            }

            // Markers must be unique
            private function goodbyeWorld():void {
            var call:Object = service.sayGoodbye();
            call.marker = "goodbyeWorld";
            }

            private function resultHandler(event:ResultEvent):void {
            var call:Object = event.token;

            if ( call.marker == "helloWorld" ) {
            // Do something with the return from sayHello call
            return;
            }

            if ( call.marker == "goodbyeWorld" ) {
            // Do something with the return from sayGoodbye call
            return;
            }
            }
            • 3. Re: AsyncToken vs Listeners
              dimival Level 1
              Yeah i saw that on the Developer's Guide, think i will do it that way.

              Thanks dishmael

              Any other ideas are welcome
              • 4. Re: AsyncToken vs Listeners
                ntsiii Level 3
                Yes, but use a switch statement instead of the if conditional. It is much cleaner when you get 10-20 different calls.

                Tracy
                • 5. Re: AsyncToken vs Listeners
                  dishmael Level 1
                  how do you use the switch statement on strings? I get an error whenever I try to switch on the string. I considered creating an enum but that seems like a lot of work for something simple. did I miss something about the switch operator?
                  • 6. Re: AsyncToken vs Listeners
                    dishmael Level 1
                    Hmmm, I went back and did the switch on a string and it works. I could've swore that the switch statement only worked on numeric values. I guess I'm getting my languages mixed up. Sorry about that, Tracy's right ... use a switch versus the 'if' statements.
                    • 7. Re: AsyncToken vs Listeners
                      dimival Level 1
                      Ok thanks very much :)