I'm using Cordova 2.1.0 and the prompt plugin located at this github. I've set up the plugin correctly with the .h, .m, and .js files, and properly linked it in my index.html.
The issue I'm having is that when the prompt function is called, it does not wait for the user to input the text string before it continues to the next function which, in my usage, saves form values to localstorage using the user text string as the ID for the storage. This problem results in a local storage key-value pair being stored with a blank key and ruins the storage system.
Here is a sample of the code I use to call the Prompt plugin:
function doPrompt() {
window.plugins.Prompt.show(
"What is the name of this job?",
function (userText) {
$('#promptinput').text(userText)
},
function () {
},
"Save",
"Cancel"
);
};
And here is a sample from where I call the prompt plugin and the function to store the values:
function showPrompt() {
doPrompt();
alert("prompted");
writeLocal();
alert("written");
}
The alerts are there so I can see when the next function is moved to and the writeLocal function is where localstorage is written to.
Any help with this would be great. I've been troubleshooting for a while trying to get this to work properly.
Yeah, you need to read up a bit on how async code works. The second argument to window.plugins.Prompt.show is a callback that gets called when the OK button is clicked. Nothing "waits" for the user to hit OK; the code is non-blocking.
You want something like this:
function doPrompt() {
window.plugins.Prompt.show(
"What is the name of this job?",
function (userText) {
$('#promptinput').text(userText);
alert("prompted");
writeLocal();
alert("written");
},
function () {
},
"Save",
"Cancel"
);
};
Now when the user hits OK it sets #promptinput, fires an alert, and then calls writeLocal. If writeLocal is also async then the second alert is fired immediatly too; otherwise it will fire when writeLocal returns.