Makoto
Makoto

Reputation: 773

is invoking a service into another service in angular

I need to save data and then I need to dispaly the changes immediately afterwards. That's why I Have a

Which is the more correct way and for which reasons ?

Should I write:

$scope.saveSaisine = function() {
   saisineSrv.updateSaisine($scope.currentSaisine.idSaisine, $scope.currentSaisine).
   then(
       function() {
           $scope.errorMessages = [];

              if ($scope.currentSaisine.idMotif) {
                 toaster.pop('success', 'Réponse', 'Success');
                 angular.element('#modalSaisine').modal('hide');

                 saisineSrv.getOneSaisine($scope.currentSaisine.idSaisine, $scope.currentSaisine).then(function(response) {
                     $scope.currentSaisine.dateModif = response.dateModif;
                 });

         },
         function error(response) {
             $scope.errorMessages = response.data;
             toaster.pop('error', 'Réponse', 'We have a problem');
         }
    );
};

OR

$scope.saveSaisine = function() {
   saisineSrv.updateSaisine($scope.currentSaisine.idSaisine, $scope.currentSaisine).
   then(
       function() {
           $scope.errorMessages = [];

              if ($scope.currentSaisine.idMotif) {
                 toaster.pop('success', 'Réponse', 'Success');
                 angular.element('#modalSaisine').modal('hide');

         },
         function error(response) {
             $scope.errorMessages = response.data;
             toaster.pop('error', 'Réponse', 'We have a problem');
         }
    );

   saisineSrv.getOneSaisine($scope.currentSaisine.idSaisine, $scope.currentSaisine).then(function(response) {
       $scope.currentSaisine.dateModif = response.dateModif;
   });
};

Upvotes: 1

Views: 26

Answers (1)

Marian Ban
Marian Ban

Reputation: 8188

the first option is a correct way how you should refresh your data because these services are asynchronous thus in the second example you may don't get fresh data (the getOneSaisine can finish before updateSaisine).

Upvotes: 1

Related Questions