Skip to content

Instantly share code, notes, and snippets.

@slywalker
Created March 29, 2012 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slywalker/2232682 to your computer and use it in GitHub Desktop.
Save slywalker/2232682 to your computer and use it in GitHub Desktop.
My.AjaxForm = function(e) {
var $e = $(e)
, id = "#" + $e.attr("id")
, redirect = $e.data("ajax-redirect")
, target = $e.data("ajax-target")
$e.ajaxSubmit({
beforeSubmit: function(arr, $form, options) {
$e.find(".btn[type=submit]").button('loading');
$e.spin();
},
success: function(responseText, statusText, xhr, $form) {
var $responseText = $(responseText)
form = $responseText.find(id).html();
if (!form) {
form = $responseText.filter(id).html();
}
if (null === form && redirect) {
$(location).attr("href", redirect);
return false;
}
$form.html(form);
if (target) {
contents = $responseText.find(target).html();
if (!contents) {
contents = $responseText.filter(target).html();
}
$(target).html(contents);
}
},
error: function(XMLHttpRequest, statusText, errorThrown) {
$e.spin();
alert(errorThrown);
location.reload();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment