Skip to content

Commit

Permalink
Adding logic to prevent forms in FormRequest submitting w/o ajax if t…
Browse files Browse the repository at this point in the history
…heir `.submit` method is called. Adding some controls to prevent flooding if this happens.
  • Loading branch information
anutron committed Dec 13, 2012
1 parent 0944a71 commit daf36ea
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions Source/Forms/Behavior.FormRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,40 @@ Behavior.addGlobalFilter('FormRequest', {
else spinner = updateElement;

if (!updateElement) api.fail('Could not find target element for form update');

var sentAt;
var req = new Form.Request(element, updateElement, {
requestOptions: {
filter: api.get('filter'),
spinnerTarget: spinner
},
resetForm: api.get('resetForm') || /* noReset is deprecated: */ !element.hasClass('noReset')
resetForm: api.get('resetForm')
}).addEvent('complete', function(){
api.applyFilters(updateElement);
}).addEvent('send', function(){
sentAt = new Date().getTime();
});
// this bit below is to throttle form submission in case more than one thing
// is trying to send it

// remove form.request submit watcher
element.removeEvent('submit', req.onSubmit);
// our new submit handler checks that requests to submit are at least 200ms apart
var submit = function(e){
if (!sentAt || sentAt + 200 < new Date().getTime()) {
req.onSubmit(e);
} else {
// if they aren't, just stop the submit event if it's present
if (e) e.stop();
}
};
// now monitor submit with our new method
element.addEvent('submit', submit);
// and overwrite the submit method on the element
element.submit = submit;
api.onCleanup(function(){
req.detach();
delete element.submit;
});
api.onCleanup(req.detach.bind(req));
return req;
}

Expand Down

0 comments on commit daf36ea

Please sign in to comment.