Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass two arguments with navigator.notification.confirm?

I'm trying to use the Phonegap notification to display an error message in my Phonegap app, and then allow the user to email the error. The only problem is I can't pass the error message to the callback function, rendering the email useless.

The code I have now looks like this:

function displayError(errormsg) {
    navigator.notification.confirm(
                                   errormsg,
                                   onConfirm,
                                   'Error',
                                   'Submit, Cancel'
                                   );
}
function onConfirm(buttonIndex){
    if (buttonIndex === 1){
        alert(errormsg);
    }

}

Which is called by displayError("Test"), which would make the error content Test. I want to then pass errormsg on to onConfirm, but I don't know how to do this, or if it's possible.

A possible solution I was considering was this:

function displayError(errormsg) {
    test = errormsg
    navigator.notification.confirm(
                                   errormsg,
                                   onConfirm,
                                   'Error',
                                   'Submit, Cancel'
                                   );
}
function onConfirm(buttonIndex){
    if (buttonIndex === 1){
        alert(test);
    }

}

But that doesn't change the errormsg if a new error is displayed. I confirmed this because in the simulator setting, my app throws two errors. The first one works fine when using that method, passing on test, but then the second error which directly follows uses the original variable, instead of the most recent one.

like image 271
Charlie Avatar asked Dec 04 '12 23:12

Charlie


1 Answers

function displayError(errormsg) {
    navigator.notification.confirm(
        errormsg,
        function(buttonIndex){
            onConfirm(buttonIndex, errormsg);
        },
        'Error',
        'Submit, Cancel'
        );
}
function onConfirm(buttonIndex, errormsg){
    if (buttonIndex === 1){
        alert(errormsg);
    }

}

What about wrapping it in an anonymous function? That way you can pass as many parameters as you like, while maintaining scope.

like image 51
ahren Avatar answered Sep 19 '22 12:09

ahren