Created
September 14, 2011 08:53
-
-
Save thbar/1216136 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#quick_entry form').live 'submit', (event) -> | |
event.preventDefault() # just moved up! | |
event.stopPropagation() # just moved up! | |
return false if @beenSubmitted | |
@beenSubmitted = true | |
clearNotice() | |
$.ajax({ | |
type: 'POST' | |
url: @action | |
dataType: 'json' | |
data: $(@).serialize() | |
success: (response) -> | |
setNotice(response.bill) | |
resetAndFocusForm() | |
error: (response) -> | |
if response.status == 422 | |
setErrorFields JSON.parse(response.responseText)['error_fields'] | |
else | |
alert("An error occurred!") | |
complete: => | |
@beenSubmitted = false | |
}) |
@dhruvasagar thanks for the link!
Could you rewrite lines 4-7 from
if @beenSubmitted
return false
else
@beenSubmitted = true
... rest of the code...
to
return false if @beenSubmitted
@beenSubmitted = true
... rest of the code...
@alexrothenberg very good point, thanks
As other commenters have suggested: Just put false
at the end rather than putting event.preventDefault()
and event.stopPropagation()
at the beginning.
You said you were concerned that if an exception is thrown, the return false
won't be reached. But unless clearNotice
might throw an exception, there's no chance of that; any error that occurs in $.ajax
and the callbacks you give it will happen asynchronously—after the function has already returned.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yea I was reading through it, I think this - http://stackoverflow.com/questions/5873221/jquery-event-bubbling-and-how-click-live-click-stoppropagation-and-ret also demonstrates the same reasonably well.