7 Replies Latest reply on Oct 31, 2009 2:51 AM by cdsvvxv

    Beginner: How to use ResultEvent?

    cdsvvxv

      I am using BlazeDS to return a Java object called User. The User object has two properties username and dob. In my ActionScript function I have:

       

      private function showReturnedUser(event:ResultEvent):void {

       

          MonsterDebugger.trace(this, "showReturnedUser [" + event.result.username.toString() + "] [" + event.result.dob.toString() + "]");

       

          var user:User = event.result.user as User;
          MonsterDebugger.trace(this, "user is [" + user + "]")

      }

       

      I get the username and dob printed out from the first debug statement but what I would like to do is to get the actual User object that was returned, currently my:

      var user:User = event.result.user as User;

       

      is null. Is that possible to get the actual User object or do I need to manually recreate the User object in Flex in order to use it? I already have the User class in Flex defined in this way:

       

      package com.adobe.remoteobject {

       

          [Bindable] [RemoteClass(alias="com.adobe.remoteobject.User")]

       

          public class User{

       

              public var username:String = "";
              public var dob:Date = new Date();

          }
      }

       

      Thanks for any suggestions.

        • 1. Re: Beginner: How to use ResultEvent?
          paul.williams Level 4

          Sounds like you're nearly there. Try:

           

          var user : User = event.result as User;

           

          Also, if you put a breakpoint into your "showReturnedUser" function you can take a closer look at the contents of your ResultEvent using the Variables view in FlexBuilder.

          • 2. Re: Beginner: How to use ResultEvent?
            cdsvvxv Level 1

            Thanks for the reply but I had tried that earlier and hadn't gotten anywhere. I guess that means the problem is elsewhere. I know the User object is returned from the server and something certainly arrives in the showReturnedUser(event:ResultEvent) function as I can print out both of:

             

            event.result.username.toString()

            event.result.dob

             

            I also tried adding getters and setters to Flex User object which now looks like this:

             

            package com.adobe.remoteobject {

             

                [Bindable] [RemoteClass(alias="com.adobe.remoteobject.User")]

             

                public class User{

             

                    public var username:String = "";
                    public var dob:Date = new Date();

             

                    public function User(username:String , dob:Date) {
                        this.username = username;
                        this.dob = dob;
                    }

             

                    public function setUsername(username:String):void {
                        this.username = username;
                    }

             

                    public function setDob(dob:Date):void {
                        this.dob = dob;
                    }

             

                    public function getUsername():String {
                        return this.username;
                    }

             

                    public function getDob():Date {
                        return this.dob;
                    }
                }
            }

             

            Sorry, I am not sure what other bits of code may be useful. One thing I noticed was that I could delete the:

             

            [Bindable] [RemoteClass(alias="com.adobe.remoteobject.User")]

             

            From the Flex User object and it made no difference - that seems a little odd.

            • 3. Re: Beginner: How to use ResultEvent?
              Colin Zhu

              I would suggest you debug this code and see what is exactly returned in the ResultEvent.

              • 4. Re: Beginner: How to use ResultEvent?
                cdsvvxv Level 1

                Unfortunatly I am not using Flex Builder and I can't see much useful in DeMonster Debugger. Can I ask if the:

                 

                [Bindable] [RemoteClass(alias="localobject.User")]

                 

                Is supossed to point to the Flex version of the User object or the Java version? I have it pointing to the Flex version as it won't compile if I set it to the Java version.

                • 5. Re: Beginner: How to use ResultEvent?
                  paul.williams Level 4

                  It should point to the Java class. What compiler error are you getting?

                   

                  Also, your ActionScript "User" class must support a no-args constructor. Otherwise the remote-object infrastructure won't be able to create instances of it. You can achieve this by making the constructor args optional:

                   

                  public function User(username:String = null , dob:Date = null)

                  {
                       this.username = username;
                       this.dob = dob;

                  }

                   

                  Since you don't have a debugger I would recommend you familiarise yourself with the trace statement or the Flex logging framework. You won't get very far without some way of inspecting the state of your application. Also take a look at Flex Spy:

                   

                  http://code.google.com/p/fxspy/

                  1 person found this helpful
                  • 6. Re: Beginner: How to use ResultEvent?
                    levancho Level 3

                    and yes for sure [RemoteClass(alias="localobject.User")] this should always point to java User class.


                     

                    I have it pointing to the Flex version as it won't compile if I set it to the Java version.

                    IMHO, this is the root of the problem ,

                    without proper mapping between as clasas and java class,

                    if you still getting result from service, it will be untyped Object, since it does not know which as Object to correspond to , and you will not be able to cas it to your User class.

                    1 person found this helpful
                    • 7. Re: Beginner: How to use ResultEvent?
                      cdsvvxv Level 1

                      Thanks to everyone for the help, it is much appreciated and I now have things working.

                       

                      This morning, as soon as I changed the User.as file from localobject.User to remoteobject.User and recompiled it ran as I had hoped. The refactoring I did last night must have corrected another error (though I am not sure what that was) and it just needed that change to finish it off.

                       

                      I will also check out the Flex Spy app as I find the DeMonster Debugger is mostly useful for traceing all though it is vry good for that.