5 Replies Latest reply on Sep 14, 2011 10:50 AM by Code Girl

    CallResponders and testing for success with Web Service

    Code Girl Level 1

      You will notice in my code below how after I save the user data that I want to set the save button enabled to false.  But what if the save fails on the server side, shouldnt I test to make sure the save passed first?  At first I thought I would create a procedure under the CallResponder which is only called when a success is performed but when I looked at the Call Responder the result property was already populated.  I guess by Flash Builder itself.  So, is something saved in the token that tells me there was success or should I create a function which does what is currently in the result property and set the save button enabled to false?  Am I correct that the result function only gets called when there is a success?

       

             <s:CallResponder id="setUserResult" result="userDto = setUserResult.lastResult as UserDto"/>
                  protected function saveBtn_clickHandler(event:MouseEvent):void
                  {
                      if (firstNameTextInput.text == "" || lastNameTextInput.text == "")
                          Alert.show(updateErrorMessage + "First and Last Name cannot be blank. " + errorActionMessage);
                      else
                      {
                          if (userDto == null)
                          {
                              userDto = new UserDto();
                              usersDDL.dataProvider.addItem(userDto);
                              usersDDL.selectedIndex = usersDDL.dataProvider.length - 1;
                          }
                          userDto.firstName = firstNameTextInput.text;
                          userDto.lastName = lastNameTextInput.text;
                          userDto.active = activeCB.selected;
                         
                          setUserResult.token = personnelBean.setUser(userDto);
                          saveBtn.enabled = false;
                      }
                  }

        • 1. Re: CallResponders and testing for success with Web Service
          Sameer Bhatt Adobe Employee

          The result of the method call depends on the implementation of the method. So, first check what is the behavior of the remote method in case of failures.

          Then, add a result event handler to the CallResponder and disable the saveBtn in the result event handler. In the event handler, you can check what the remote method returned and then decide whether to disable the button.

          • 2. Re: CallResponders and testing for success with Web Service
            Code Girl Level 1

            Maybe I need to rephrase my question then

             

            I googled for the answer before I ever posted my question.  I looked for the answer to my question in the book which I was trying to learn Flash Builder and it did not give me an answer.  I looked at the token and such and It was not obvious how we check for success or failure.  I am asking how we check for success or failure.  So it would have been nice if you gave an example so that I would not have had to ask a second time.  Or at least stated a function to point me in the right direction.  Just telling me to check for failure didnt help me at all.  Thats kinda obvious.  So would someone please help me find a solution to my question.  Where do I check for failure?  In the token?  What flags are set?  For example, lets say the network goes down.  Surely there is something set somewhere so I can gracefully respond to the error rather than letting the app just fail.

            • 3. Re: CallResponders and testing for success with Web Service
              Sameer Bhatt Adobe Employee

              Consider this code snippet -

              <fx:Script>
                      <![CDATA[
                          
                          import mx.rpc.events.FaultEvent;
                          import mx.rpc.events.ResultEvent;
                      
                          protected function resultHandler(event:ResultEvent):void
                          {
                              // TODO Auto-generated method stub
                              
                          }
                          
                          protected function faultHandler(event:FaultEvent):void
                          {
                              // TODO Auto-generated method stub
                              
                          }
                          
                      ]]>
                  </fx:Script>
              
              <fx:Declarations>
                      <!-- Place non-visual elements (e.g., services, value objects) here -->
                      <s:CallResponder result="resultHandler(event)" fault="faultHandler(event)" />
              </fx:Declarations>
              

               

              So, when there is a failure (such as network failure), a fault event will be dispatched and faultHandler() will be called. If everything goes fine, then result event will be dispatched and resultHandler() will be called.

               

              Hope this helps.

               

              Message was edited by: Sameer Bhatt

              • 4. Re: CallResponders and testing for success with Web Service
                Code Girl Level 1

                So are you saying that the result handler is only called if there is only success?

                • 5. Re: CallResponders and testing for success with Web Service
                  Code Girl Level 1

                  I looked up CallResponder and saw that Result is only called on Success.  I had interpreted Result as it returning the result of the call.  Good or bad.  Success or failure.  Funny that if there is a failure it calls failure but if it returns a success it calls result.  At least I finally found the answer to my question.  Also, for those who is reading this thread.  If you put an fault alert on the service itself then the error is handled for all call responders for that service.

                   

                          <servicebean:ServiceBean id="serviceBean"
                                                       fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                                       showBusyCursor="true"/>