AngularJS validation and promises

2.1k views Asked by At

In an Angular app, form is validated via custom JS function before submitting. Based on a condition, I need to show a confirmation dialog to the user, wait for confirmation or rejection, then proceed with validation. I'm having difficulties with achieving this asynchronously. I believe I am not using the promise correctly, but not sure what needs to be updated.

Factory

app.factory("confirmDialogService", ['$q', 'ngDialog',
  function ($q, ngDialog ) {
  return {
    popConfirm: function () {
        var deferred = $q.defer();
        var confirmed;
        setTimeout(function () {
                ngDialog.openConfirm({
                    template: 'template/confirmDialog.html',
                    className: 'ngdialog-theme-default'
                }).then(function (value) {
                    console.log('Modal promise resolved. Value: ', value);
                    deferred.resolve(confirmed =  true);
                }, function (reason) {
                    console.log('Modal promise rejected. Reason: ', reason);
                    deferred.reject(confirmed =  false);
                });
        }, 1000);
        return deferred.promise;
      }
  };
}]);

Controller

  $scope.checkSomething = function (){
    if(needsConfirmation)
    {
        console.log('about to pop confirm');
        confirmationService.popConfirm().then(function(confirmed){
            console.log( 'from popConfirm() ' + confirmed + ''); // never logged
            return confirmed;
        }, function(){
        // something went wrong
            return false;
        });
    }
    else
        return true;
}

The confirmation dialog is shown, but clicking yes or no in the dialog does not produce results as expected.

What I'd like to be able to do is get a true/false value depending whether a user confirmed or dismissed the dialog, along the lines of

var canProceed = $scope.checkSomething();

I think I need to wrap this into another promise but not sure how. Any help will be greatly appreciated.

Thanks,

2

There are 2 answers

3
dfsq On BEST ANSWER

Since openConfirm already returns a Promise, you don't need to create one more with $q.defer():

app.factory("confirmDialogService", ['$q', 'ngDialog', function ($q, ngDialog) {
    return {
        popConfirm: function () {
            return ngDialog.openConfirm({
                template: 'template/confirmDialog.html',
                className: 'ngdialog-theme-default'
            }).then(function (value) {
                console.log('Modal promise resolved. Value: ', value);
                return true;
            }, function (reason) {
                console.log('Modal promise rejected. Reason: ', reason);
                return false;
            });
        }
    };
}]);

After that checkSomething would use it this way:

$scope.checkSomething = function () {
    if (needsConfirmation) {
        return confirmationService.popConfirm().then(function (confirmed) {
            return confirmed;
        }, function () {
            return false;
        });
    } else {
        return $q.when(true);
    }
}

Note how it no longer returns true or false, but rather a promise object. Finally you should use $scope.checkSomething() like asynchronous operation with promises:

$scope.checkSomething().then(function() {
    // ok, proceed
}, function() {
    // something failed
});

Summarizing, the most important thing to understand it that checkSomething should return a promise and not just true/false.

0
Rasalom On

You don't need variable to pass it in resolve and reject methods(but it will work):

           .then(function (value) {
                console.log('Modal promise resolved. Value: ', value);
                deferred.resolve(true);
            }, function (reason) {
                console.log('Modal promise rejected. Reason: ', reason);
                deferred.reject(false);
            });

then in controller:

$scope.checkSomething = function (){
    if(needsConfirmation)
    {
        console.log('about to pop confirm');
        confirmationService.popConfirm().then(function(confirmed){
            console.log( 'from popConfirm() ' + confirmed + ''); // never logged
            return confirmed; <-- this is not(!) a return of checkSomething method, this is return of success callback
        }, function(){
        // something went wrong
            return false; //same as above
        });
    }
    else {
        return true;
    }
}

so you can't do var canProceed = $scope.checkSomething();

you need to set this variable inside callbacks:

            var canProceed;
            confirmationService.popConfirm().then(function(confirmed){
                canProceed = true;
            }, function(){
            // something went wrong
                canProceed = false;
            });