SuperFunMan
SuperFunMan

Reputation: 175

Phonegap Navigator Notification Confirmback not working in sequence

The app has a button to delete a dblog. When the user clicks on the button, I ask for a confirmation and then go ahead and delete.

I am using navigator.notification.confirm to ask the user

Here is my code -

function deleteLog()
{

    navigator.notification.confirm(
    "Are you sure you want delete?",
    function(buttonIndex){
        if(buttonIndex==1)
        {
            console.log("User has cancelled");
            return;
        }
    },
    "Confirmation",
    "Cancel, Delete");

    console.log("User has confirmed Delete");


}

However, even before the user clicks on Cancel or Delete, I get "User has confirmed" message. I tried adding a else statement above, and still no luck.

What could be wrong?

EDIT: Some more information -

The sequence I like is Single Thread === Press Delete -> Ask for Confirmation -> User Pressed Delete -> Delete dbLog.

What is happening Press Delete --> Thread One == Ask for Confirmation Thread Two (after Press Delete) --> Deleting DB Log

Upvotes: 1

Views: 1969

Answers (1)

gcatalfamo
gcatalfamo

Reputation: 343

By putting there console.log("User has confirmed Delete"), out of the callback, you are basically telling the program to run the console.log whatever the user presses.

I would take the confirmation function out for better usability and write it like this:

function deleteLog() {
    navigator.notification.confirm(
        'Are you sure you want delete?', // message
        onConfirm,                       // callback to invoke with index of button
        'Confirmation',                  // title
        'Cancel,Delete'                  // buttonLabels
    );
}

//on button press, the onConfirm function is called
function onConfirm(button) {
    //console.log('You selected button ' + button);
    if(button == 1){
        //pressed "cancel"
        console.log("User has cancelled");
    }
    else if(button == 2){
        //pressed "delete"
        console.log("User has confirmed Delete");
    }
}

This works.

Upvotes: 2

Related Questions