onSubmit callback for formSubmit

if you don't like success/error callbacks and instead want a deferred
object, use this callback. your function will receive a deferred object
encompassing the request(s) triggered by the submit, and the formData
being posted

test plan:
N/A, see #7277... this will be used by that code

Change-Id: I070f3fb33169256eb151919d820a1b4f0010e4f3
Reviewed-on: https://gerrit.instructure.com/13615
Reviewed-by: Zach Pendleton <zachp@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
This commit is contained in:
Jon Jensen 2012-09-11 08:58:01 -06:00
parent 7e398a62a5
commit e2dcea8472
1 changed files with 13 additions and 2 deletions

View File

@ -61,6 +61,9 @@ define([
// fileUpload: Either a boolean or a function. If it is true or
// returns true, then it's assumed this is a file upload request
// and we use the iframe trick to submit the form.
// onSubmit: A callback which will receive 1. a deferred object
// encompassing the request(s) triggered by the submit action and 2. the
// formData being posted
$.fn.formSubmit = function(options) {
this.submit(function(event) {
var $form = $(this); //this is to handle if bind to a template element, then it gets cloned the original this would not be the same as the this inside of here.
@ -100,13 +103,21 @@ define([
}
if (options.disableWhileLoading) {
var oldOnSubmit = options.onSubmit || function(){};
options.onSubmit = function(loadingPromise) {
$form.disableWhileLoading(loadingPromise);
oldOnSubmit.apply(this, arguments);
}
}
if (options.onSubmit) {
var loadingPromise = $.Deferred(),
oldHandlers = {};
$form.disableWhileLoading(loadingPromise);
options.onSubmit(loadingPromise, formData);
$.each(['success', 'error'], function(i, successOrError){
oldHandlers[successOrError] = options[successOrError];
options[successOrError] = function() {
loadingPromise[successOrError === 'success' ? 'resolve': 'reject']();
loadingPromise[successOrError === 'success' ? 'resolve': 'reject'].apply(loadingPromise, arguments);
if ($.isFunction(oldHandlers[successOrError])) {
return oldHandlers[successOrError].apply(this, arguments);
}