Reputation: 27972
I'm trying to write a function which opens an angular-ui modal window and returns a promise once it's closed. This is normally easy, you ust return the modal instance, but in this scenario even if the window is closed property I want to check the response inside the .then() function and possibly reject the promise based on a status code, even though it has been successful.
That probably doesn't make much sense so here's some code...
// Function which should return a promise
var myFunc = function(){
var modalInstance = $modal.open({
templateUrl: 'xxx',
controller: 'yyy',
});
return modalInstance.result.then(function (result) {
// If the login was successful then resolve
if(result && result.success === true){
// What goes here?
}
// If it was uncuccessful then reject, even though it was closed successfully.
else{
// What goes here?
}
}, function () {
$log.info('Modal was closed unsuccessfully');
});
}
myFunc.then(function(){
// DO something
});
Upvotes: 1
Views: 1772
Reputation: 827
You can return a new promise that will only get resolved if the promise from $modalInstance.result gets resolved AND your status code check is good. Something along the lines of:
var myFunc = function(){
var modalInstance = $modal.open({
templateUrl: 'xxx',
controller: 'yyy',
});
var deferred = $q.defer();
modalInstance.result.then(function() {
if (/* status code checks ok */) {
deferred.resolve();
}
else {
deferred.reject();
}
}, function() {
deferred.reject();
});
return deferred.promise;
}
See the docs on $q.
Upvotes: 2