3

I have a function $scope.save(). Here am calling another function $scope.validateFormat()just to validate some value Inside validate function am making one Api call and checking the result if value is valid am returning and allowing the save to save my value. If invalid am setting the value setvalidity as false and throwing error. My doubt here is when i tried to call validateFormat() before completing this function save function is getting complete.

      $scope.save() = function(){
        // some code
        $scope.validateFormat();// calling the validate function
       // codde for saving value

       }
      $scope.validateFormat(){
      // some API to get result
      if(success){
      // some code
       }
     else{
      $scope.filestatus.editFileField.$setValidity("fileField", false);
       return false;
        }

}

JOGO
  • 285
  • 2
  • 7
  • 16
  • Not sure if that's the case, but you should probably be using async calls instead. Use a callback when `validateFormat()` is complete and after the callback is executed perform the save. – pgrodrigues Dec 16 '15 at 10:58
  • The save function completes before the validateFormat function completes? My guess is that the API call is async. That's why the save function proceeds without waiting for the validateFormat function to end. You could add a while-loop that waits for the validateFormat function to end. – Mtihc Dec 16 '15 at 11:04
  • or he can use a callback. – Rishi Dec 16 '15 at 11:05
  • thanks for your quick thought.. Let me try both the ways. – JOGO Dec 16 '15 at 11:08

2 Answers2

4

As I mentioned in the comment, here's an example using callbacks:

$scope.save = function(){
    // some code
    $scope.validateFormat(function (data) {
        if(!data.err) {
            // code for saving value
        }
    });
}

$scope.validateFormat = function(callback){
    // some API to get result
    $http.get('/something').then(function (success, error) {
        if(success){
            // some code
            callback({err: false});
        }
        else{
            $scope.filestatus.editFileField.$setValidity('fileField', false);
            callback({err: true});
        }
   });
}

You should adapt this according to your needs.

For more info regarding callbacks in javascript take a look at this answer.

Community
  • 1
  • 1
pgrodrigues
  • 2,083
  • 1
  • 24
  • 28
  • here using callback dint solve this problem. Cos validateFormate() function am calling inside a specific condition within save function. $scope.save= function(){ if(fieldVal=="NO"){ validate(); } So i can't block the entire save code just for a specific case. any other thought? } – JOGO Dec 16 '15 at 11:28
2

I think you should create promises here using $q service.

          $scope.validateFormat = function(callback){
               var deferred = $q.defer(); 
            // some API to get result
            $http.get('/something').success(function(data){ 
                 deferred.resolve(data);
        .error(function(err){
          $scope.filestatus.editFileField.$setValidity('fileField', false);
                 deferred.reject(err)});
                 return deferred.promise(); 
         }
Nadeem Khoury
  • 886
  • 6
  • 18