I have the following code
if (testNavigation() && $scope.selections.somethingChanged) {
return false;
}
in the testNavigation I am calling modal dialog and if I answer Ok in that dialog, I re-set somethingChanged to false. My problem is that when the code is executed, the testNavigation and modal dialog is by-passed and executed later and therefore my test is not working as I need it to work. What should I change in order for my logic to properly work, e.g. first invoke my modal dialog, and proceed accordingly in the main function?
This is my testNavigation method:
var testNavigation = function()
{
if ($scope.selections.somethingChanged) {
var modal = $modal.open({
controller: function ($scope) {
$scope.okClass = 'btn-primary',
$scope.okLabel = resourceFactory.getResource('Labels', 'yes'),
$scope.cancelLabel = resourceFactory.getResource('Labels', 'cancel'),
$scope.title = resourceFactory.getResource('Labels', 'unsavedChanges'),
$scope.message = resourceFactory.getResource('Messages', 'unsavedChanges');
},
templateUrl: '/app/templates/modal'
});
modal.result.then(function () {
$scope.selections.somethingChanged = false;
});
}
return true;
}
I'll try to add more details. I have LoadView() and New() functions in the Index page controller. In both of these functions I need to do the following:
if $scope.selections.somethingChanged = false I need to proceed with the logic.
if $scope.selections.somethingChanged = true I need to pop up a modal dialog asking if I want to go ahead and Cancel my changes or Stay on the current page. If I answer Yes, I want to go ahead with the logic.
So, that's the purpose of the separate testNavigation function. In the languages where each function call is sequential, that would work as I intended. But it doesn't work this way in AngularJS / JavaScript and I am not sure how to make it to work the way I need. We tried few ideas with $q service but didn't get the result.
testNavigation(). Regardless, you will need to use a promise (or a callback).testNavigation()to show the modal, and have a separate handler for when "Ok" is clicked.