Simplify code to handle ajax popover and redirects

This commit is contained in:
Frederic Guillot
2016-01-30 22:25:16 -05:00
parent 4a52d327f7
commit bb040cfb78
27 changed files with 119 additions and 182 deletions

File diff suppressed because one or more lines are too long

View File

@@ -57,6 +57,7 @@ Popover.prototype.afterOpen = function() {
var self = this;
var popoverForm = $(".popover-form");
// Submit forms with Ajax request
if (popoverForm) {
popoverForm.on("submit", function(e) {
e.preventDefault();
@@ -66,18 +67,35 @@ Popover.prototype.afterOpen = function() {
url: popoverForm.attr("action"),
data: popoverForm.serialize(),
success: function(data, textStatus, request) {
var redirect = request.getResponseHeader("X-Ajax-Redirect");
if (redirect) {
window.location = redirect === 'self' ? window.location.href : redirect;
}
else {
$("#popover-content").html(data);
$("input[autofocus]").focus();
self.afterOpen();
}
self.afterSubmit(data, request, self);
}
});
});
}
// Submit link with Ajax request
$(document).on("click", ".popover-link", function(e) {
e.preventDefault();
$.ajax({
type: "GET",
url: $(this).attr("href"),
success: function(data, textStatus, request) {
self.afterSubmit(data, request, self);
}
});
});
};
Popover.prototype.afterSubmit = function(data, request, self) {
var redirect = request.getResponseHeader("X-Ajax-Redirect");
if (redirect) {
window.location = redirect === 'self' ? window.location.href : redirect;
}
else {
$("#popover-content").html(data);
$("input[autofocus]").focus();
self.afterOpen();
}
};