5 Replies Latest reply on Aug 11, 2010 3:10 PM by j.ochoa

    How to access method argument from result/fault handler?

    j.ochoa Level 1

      I have a rather general Flex question.  First, let me describe the scenario:

       

      In a controller class, DocumentController, I have a method "deleteDocument" that accepts a DocumentVO class.  This method simply delegates the call to a service delegate and adds result and fault handlers with the help of a service "helper".

       

      public function deleteDocument(document : DocumentVO) : void

      {

           var call : AsyncToken = documentDelegate.deleteDocument(document);

       

           serviceHelper.executeServiceCall(call, deleteDocumentResultHandler, deleteDocumentFaultHandler);

      }

       

      The result and fault handler methods look something like this:

       

      private function deleteDocumentResultHandler(event : ResultEvent) : void

      {

           // do something

       

      }

       

      private function deleteDocumentFaultHandler(event : ResultEvent) : void

      {

       

           // do something

      }

       

      My question:

      In my result and fault handlers, how can I get access to the "document" object that was originally passed to the deleteDocument method (so that I can access its properties to do additional processing)?  One way that comes to mind is to assign it to an instance variable of the DocumentController class in the deleteDocument method (before making the service call), but this seems more like a hack to me.  Unfortunately, the object isn't part of the ResultEvent passed back to the handlers.  Is there another, more elegant, way to get access to the document object?

       

      Thanks,

      -Jose

        • 1. Re: How to access method argument from result/fault handler?
          mike_morales Level 2

          I was going to suggest what you were thinking, "assign it to an instance variable of the DocumentController class in  the deleteDocument method (before making the service call), but this  seems more like a hack to me"

           

          Just curious why you would consider that a hack?  You could also dispatch a custom event from your controller (in your result handlers) that would request the original VO object from your main app.

          • 2. Re: How to access method argument from result/fault handler?
            UbuntuPenguin Level 4

            I guess there is a more elegant way , it depends on what you consider elegant.  I don't know anything about your service helper , but your usage of tokens can be of use.  Since the token is a dynamic object ,  you can set variables on it.  So you could set the document on your token.  However , it seems as if you have some abstraction between the token and the call , meaning this really wouldn't be possible without shifting some stuff around.

             

             

            Generally , when I use delegates and helpers , they perform their operation , and then let the rest of the application know whether it was a success or failure.  This way , you maintain the reference to your original object ( the document ) and still keep some semblance of control flow.  The added benefit is that if you put the service inside of the delegate , or outside of the document controller , you decrease coupling and lend your code to unit testing.

             

            Sincerely ,

              Ubu.

             

            P.S Also , using delegates to do the heavy lifting ( service call and response ... ) as opposed to keeping a reference to the deleted document in the document controller allows you to delete more than one document asynchronously.  Then you don't have to worry about matching calls and events to documents , should your application warrant it.

            • 3. Re: How to access method argument from result/fault handler?
              j.ochoa Level 1

              @Mike:

               

              Perhaps "hack" was a poor word choice.  It's definitely a viable solution -- and one I'll keep in mind -- but thought there might be a better way to manage it.

               

              Thanks for the suggestions,

              -Jose

              • 4. Re: How to access method argument from result/fault handler?
                j.ochoa Level 1

                @Ubuntu:

                 

                I'm using the Swiz framework and this is a common way to decouple the controller from the service.  However, it's not specific to Swiz, which is why I posted here instead of their user group.

                 

                For my specific example, the service call will delete the document from the backend datastore.  If successful, the deleteDocumentResultHandler will then get invoked and be responsible for removing the item from the display (a datagrid) by removing its element from the list bound to the datagrid, which is held in the model.  Obviously, to maintain this loose coupling and for unit testing, my service delegate shouldn't be responsible for removing the item from the list.

                 

                Given this additional information, would you suggest storing it as an instance variable of the DocumentController class or adding it to the token (how would I do that)?

                 

                Thanks,

                -Jose

                • 5. Re: How to access method argument from result/fault handler?
                  j.ochoa Level 1

                  Turns out the Swiz service helper's executeServiceCall() is able to accept pass-through variables that are then passed to the result/fault handlers, which is exactly what I need.

                   

                  Thanks for the help, guys!

                  -Jose