I am having this issue with an async function. The point is that the "execution" is not waiting for the return of NotificationService.confirm callback.
I mean, that shows a PhoneGap alert, but it's not waiting for evaluate which button pressed the user. So, you can see in the console output undefined instead of false/true/3 values
[EDIT]
This is the code proposed by Maxim Shoutin, but it isn't working yet:
NotificationController.js
angular.module('app').controller("NotificationController", function($rootScope) {
$rootScope.cancel_button = function() {
var confirm = NotificationService.confirm("Do you want to confirm?", 'Yes!');
confirm.then(function(result) {
console.log('Confirm: ' + result);
if(confirm) $location.path('/menu');
}, function(result) {
console.log('No data returned');
})
}
/* Additional controller code... */
}
NotificationService.js
angular.module('app').factory("NotificationService", function() {
// Callback function
var onConfirm = function(button) {
console.log('Callback function called!!!');
if(button == 1) return false;
else if(button == 2) return true;
else if(button == 3) return 3;
else return false; // dismissed without press button
};
return {
confirm : function(alert_msg, title, buttonsArray) {
var deferred = $q.defer();
if(buttonsArray == null) {
buttonsArray = ['Cancel', 'OK'];
}
var data = navigator.notification.confirm(
alert_msg, // message
onConfirm, // callback
title, // title
buttonsArray // buttonsArray
);
deferred.resolve(data);
return deferred.promise;
}
}
}
CONSOLE OUTPUT
> Confirm: undefined (BEFORE user pressed the button)
> Callback function called!!! (AFTER user pressed the button)
The problem is you're resolving the promise before the data has actually come back. You need to give your onConfirm function access to the promise that NotificationService.confirm is handing back, and only call resolve inside there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With