user1859281
user1859281

Reputation: 1

Call a function with an AlertDialog

I've just started learning javascript and i'm currently making a small notepad like app. When i save the text it's saved to an uneditable text area on a seperate window.

I'd like to add a confirmation alert window to my app. When the "submit" button is pressed it should open an alert with two buttons (Confirm, Cancel).

"Confirm" should save the textArea text as the submit button currently does and "Cancel" should cancel any actions. I managed to find one example of this but being the newbie that i am i couldn't implement it without errors. Got this code:

submitButton.addEventListener("click", function (e) {
    if (textArea.value != "") {
        var newFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "newFile.txt");

        if (!newFile.exists()) {
            newFile.write();
            newFile.write(textArea.value);
            textArea02.value = textArea.value;
        } else {
            var fileContent = newFile.read();
            var newContent = fileContent.text + " " + textArea.value;

            newFile.write(newContent);
            textArea02.value = newContent;

            alert("File Saved");
        }

        textArea.value = "";
        textArea.blur();

    } else {
        alert("Enter some text to save");
    }
})

Upvotes: 0

Views: 134

Answers (1)

Dawson Toth
Dawson Toth

Reputation: 5680

var alertDialog = Ti.UI.createAlertDialog({
    title: 'Confirm',
    message: 'Are you sure?',
    buttonNames: [ 'No', 'Yes' ],
    cancel: 0 // index to the cancel button
});
alertDialog.addEventListener('click', function (evt) {
    if (evt.index /* if it's 1, they hit Yes */) {
        alert('OK!');
    }
});
alertDialog.show();

Upvotes: 0

Related Questions