2

I am using jQuery prompt to prompt a user to save. So, a user clicks a Save button, the Jquery prompt displays and asks if the user is sure he/she wants to save changes or not. The problem is, the user is able to click multiple times on the jQuery Save prompt button before the prompt closes. As a result, my save routine runs once for each time the prompt save button is clicked, which results in duplicate records in the database. I need to disable the prompt save button on the first click or simply close the prompt window on the first click to prevent multiple saves. How can I do this?

Client code:

$.prompt("Are you sure you want to save? You will not be able to change your decision after it's been saved.", {
    title: "Save?",
    buttons: { "Save": true, "Cancel": false },
    submit: function (e, v, m, f) {
        if (v) {
            response = saveUpdates(LoanDetails);
        }
    }
});
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Chris
  • 795
  • 2
  • 12
  • 27

2 Answers2

0

Try to use a global variable as a flag, or disable the prompt button (if you are using a button to call the prompt) $("#promptbutton").prop("disabled",true);

Edit:

The easier solution is to simply close the prompt right before calling the saveUpdates function, or at the start of the function:

jQuery.prompt.close();
Trader
  • 249
  • 4
  • 10
  • This is better suited as a comment since it does not provide an answer to the OP's question. BTW, if you click on the tags in the questions they may lead you to the plugin. :) – Jay Blanchard May 16 '14 at 14:04
  • Oh sorry, I didn't noticed the tag leading to the plugin, I will edit my answer. Thanks – Trader May 16 '14 at 14:06
  • @JayBlanchard, what is Trader supposed to do since he doesn't have enough rep to add comments? – Scott Murphy May 16 '14 at 14:10
  • Honestly @ScottMurphy he is supposed to do enough to earn enough rep to post comments. It has been discussed ad nauseum on http://meta.stackoverflow.com/ – Jay Blanchard May 16 '14 at 14:13
  • i tried putting jQuery.prompt.close(); inside the if (v) but did not work. – Chris May 16 '14 at 14:29
0

I created a fiddle here: http://jsfiddle.net/smurphy/4fqjR/

The solution is a little convoluted. I thought there would be a simpler way to do it but after looking through the documentation I cannot find any built in feature.

Here is the code:

submit: function (value, message) {
        if (value) {
            $(message.prevObject)
               .find('button[value="true"]')
               .attr('disabled', true);

            //TODO: perform save 
            //response = saveUpdates(LoanDetails);
        }
    }

The message param is a reference to the text description so you can find the button by navigating around the DOM from that starting point.

Scott Murphy
  • 448
  • 2
  • 12