{"action":"created","issue":{"url":"https://api.github.com/repos/trentrichardson/jQuery-Impromptu/issues/55","labels_url":"https://api.github.com/repos/trentrichardson/jQuery-Impromptu/issues/55/labels{/name}","comments_url":"https://api.github.com/repos/trentrichardson/jQuery-Impromptu/issues/55/comments","events_url":"https://api.github.com/repos/trentrichardson/jQuery-Impromptu/issues/55/events","html_url":"https://github.com/trentrichardson/jQuery-Impromptu/issues/55","id":58230073,"number":55,"title":"Valid form by Enter key with ajax call inside, call 2 times the submit callback !","user":{"login":"deguich","id":7642281,"avatar_url":"https://avatars.githubusercontent.com/u/7642281?v=3","gravatar_id":"","url":"https://api.github.com/users/deguich","html_url":"https://github.com/deguich","followers_url":"https://api.github.com/users/deguich/followers","following_url":"https://api.github.com/users/deguich/following{/other_user}","gists_url":"https://api.github.com/users/deguich/gists{/gist_id}","starred_url":"https://api.github.com/users/deguich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/deguich/subscriptions","organizations_url":"https://api.github.com/users/deguich/orgs","repos_url":"https://api.github.com/users/deguich/repos","events_url":"https://api.github.com/users/deguich/events{/privacy}","received_events_url":"https://api.github.com/users/deguich/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-02-19T16:19:48Z","updated_at":"2015-02-24T18:08:12Z","closed_at":null,"body":"When I use the enter key to valid a form and doing an ajax call in the submit callback. It call a second time the submit callback. \r\n\r\nI build this small test with these files :\r\nhttps://filesender.renater.fr/?vid=61d10bb3-1df0-37c9-2bef-000036df7ed9\r\nI put the ajax async = false properties to reproduce easily this strange behaviour. When I click directly on \"Work\" button, it works fine. I used console.log function to trace calls."},"comment":{"url":"https://api.github.com/repos/trentrichardson/jQuery-Impromptu/issues/comments/75811657","html_url":"https://github.com/trentrichardson/jQuery-Impromptu/issues/55#issuecomment-75811657","issue_url":"https://api.github.com/repos/trentrichardson/jQuery-Impromptu/issues/55","id":75811657,"user":{"login":"trentrichardson","id":313144,"avatar_url":"https://avatars.githubusercontent.com/u/313144?v=3","gravatar_id":"","url":"https://api.github.com/users/trentrichardson","html_url":"https://github.com/trentrichardson","followers_url":"https://api.github.com/users/trentrichardson/followers","following_url":"https://api.github.com/users/trentrichardson/following{/other_user}","gists_url":"https://api.github.com/users/trentrichardson/gists{/gist_id}","starred_url":"https://api.github.com/users/trentrichardson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trentrichardson/subscriptions","organizations_url":"https://api.github.com/users/trentrichardson/orgs","repos_url":"https://api.github.com/users/trentrichardson/repos","events_url":"https://api.github.com/users/trentrichardson/events{/privacy}","received_events_url":"https://api.github.com/users/trentrichardson/received_events","type":"User","site_admin":false},"created_at":"2015-02-24T18:08:12Z","updated_at":"2015-02-24T18:08:12Z","body":"Typically when I do asyncronous work, such as ajax I will create another state that may have a spinner gif for \"Waiting\".  I will also include one called errors sometimes where I inject any custom errors.  Of course you can always update the main state and go back to it.\r\n\r\n```js\r\nvar states = [\r\n\t\t{\r\n\t\t\tname: 'state0',\r\n\t\t\ttitle: 'Create Project',\r\n\t\t\thtml: '... html string ...',\r\n\t\t\tbuttons: { Create: true, Cancel: false },\r\n\t\t\tsubmit: function(e,v,m,f){\r\n\t\t\t\tif(v){\r\n\t\t\t\t\te.preventDefault();\r\n\t\t\t\t\tvar err = [];\r\n\r\n\t\t\t\t\t// do validation\r\n\t\t\t\t\tif(f.name === ''){\r\n\t\t\t\t\t\terr.push('Please enter a name');\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t\tif(err.length > 0){\r\n\t\t\t\t\t\t$.prompt.getState('error').find('.errors').html(err.join('<br />')));\r\n\t\t\t\t\t\t$.prompt.goToState('error');\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse{\r\n\r\n\t\t\t\t\t\t$.prompt.goToState('wait');\r\n\r\n\t\t\t\t\t\t// do ajax save\r\n\t\t\t\t\t\t$.post('file.html', f, function(res){\r\n\t\t\t\t\t\t\tif(res && res.errors && res.errors.length > 0){ // error was returned\r\n\t\t\t\t\t\t\t\t$.prompt.getState('error').find('.errors').html(res.errors.join('<br />')));\r\n\t\t\t\t\t\t\t\t$.prompt.goToState('error');\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\telse{ // ajax was successful\r\n\t\t\t\t\t\t\t\t$.prompt.close();\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t},'json');\r\n\t\t\t\t\t}// end err.length\r\n\t\t\t\t}// end if(v)\r\n\t\t\t}\r\n\t\t},\r\n\t\t{\r\n\t\t\tname: 'wait',\r\n\t\t\ttitle: 'Saving Project',\r\n\t\t\thtml: 'Please wait as the project is saved&hellip;',\r\n\t\t\tbuttons: {}\r\n\t\t},\r\n\t\t{\r\n\t\t\tname: 'error',\r\n\t\t\ttitle: 'An Error Occured',\r\n\t\t\thtml: '<p class=\"errors\">A error may have prevented this from being saved.</p>',\r\n\t\t\tbuttons: { Close: true }\r\n\t\t}\r\n\t];\r\n$.prompt(states);\r\n```\r\n\r\nI have no clue if that works, I just whipped it up to demonstrate.  This way you don't try to conditionally allow the form to submit, you will always go to another state or close.  Ajax validation and saving would work the same way.  I hope that makes sense, sorry if not...  "}}