2 Replies Latest reply on Feb 23, 2011 4:22 PM by eye_j

    Problem with dispatching events, TypeError #1034

    eye_j

      Hi guys,

       

      I`m trying to develop a multitier Flex app and i have one problem with events. Maybe its simple but i`m bumping my head and cannot find an answer.

       

      My application will use AMFPHP to exchange data with the server. Since, i will have a lot of PHP services, i decided to create one RemoteService class, which will handle communication to the server (create and manage AMF channels, introspects error messages from the server..). RemoteService class will be the base class for the Service classes (SecurityService, UserService...) which will implement specific logic but the communication part will inherit from the base class.

       

      So, my problem is this: On mxml form, i create UserService and call its method GetUsers(). UserService initiate the call to the server through its base class. When i get the response in UserService class, i want to dispach a custom RemoteServiceEvent which will transfer a list of users wrapped in one value object (named AsVO).

       

      So, when i try to dispatch the event from UserService class to the mxml form like this:

       

       

      protected function onUserListResult(event:ResultEvent):void

      {

         var result:AsVO = ClassConverter.AmfToAsConverter(event.result as AmfVO);

         dispatchEvent(new RemoteServiceEvent(result, RemoteServiceEvent.RESULT));

      }

       

       

      i get the following error:

       

      TypeError: Error #1034: Type Coercion failed: cannot convert my.domain.events::RemoteServiceEvent@95b6ac1 to mx.rpc.events.ResultEvent.

       

      On the form, i have the following code:

       

       

      userService = new UserService();

      userService.addEventListener(RemoteServiceEvent.RESULT, handleResult);

      userService.getUserList();

       

       

      If i comment the line

       

      userService.addEventListener(RemoteServiceEvent.RESULT, handleResult);

       

      i dont get an error.

       

      Any help would be appreciated.

        • 1. Re: Problem with dispatching events, TypeError #1034
          robglobal Level 1

          You should be able to fix it pretty simply by changing this:

           

          protected function onUserListResult(event:ResultEvent):void

           

           

          to this:

           

          protected function onUserListResult(event:RemoteServiceEvent):void

           

           

          1 person found this helpful
          • 2. Re: Problem with dispatching events, TypeError #1034
            eye_j Level 1

            Now i see i`ve made  a confusing explanation. Sorry for that.

             

            UserService class has event handler defined like i already wrote:

             

            protected function onUserListResult(event:ResultEvent):void

             

            because this function is event handler of RemoteObject defined in RemoteService class. Like i already said, i defined one superclass to maintain communication related stuff, and when the subclasses initiate the call to the remote methods, they use RemoteObject. So, in UserService, i have:

             

            public function getUserList():void

            {

               remoteObject.getUserList.addEventListener(ResultEvent.RESULT, onUserListResult);

               remoteObject.getUserList();

            }

             

            Since here is ResultEvent type of event,

             

            protected function onUserListResult(event:ResultEvent):void

            {

               var result:AsVO = ClassConverter.AmfToAsConverter(event.result as AmfVO);

               dispatchEvent(new RemoteServiceEvent(result, RemoteServiceEvent.RESULT));

            }

             

            is ok. As the matter of fact, i do get the list of users, and i wrap it successfully in AsVO, UserService code is ok. I get the error when i try to forward this object to the mxml form, by dispatching the custom event.

             

            Thanks!