From 23f218ee17e49b4eaf6fb310395eaba48732f812 Mon Sep 17 00:00:00 2001 From: Konstantin Koulechov Date: Tue, 13 Feb 2018 15:25:58 +0100 Subject: [PATCH 1/2] Try submit form instead of go to action URL when POST on failure --- jquery.pjax.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/jquery.pjax.js b/jquery.pjax.js index d57269d4..12645879 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -123,7 +123,8 @@ function handleSubmit(event, container, options) { type: ($form.attr('method') || 'GET').toUpperCase(), url: $form.attr('action'), container: $form.attr('data-pjax'), - target: form + target: form, + push: false, } if (defaults.type !== 'GET' && window.FormData !== undefined) { @@ -139,7 +140,6 @@ function handleSubmit(event, container, options) { // Fallback to manually serializing the fields defaults.data = $form.serializeArray() } - pjax($.extend({}, defaults, options)) event.preventDefault() @@ -244,7 +244,7 @@ function pjax(options) { var allowed = fire('pjax:error', [xhr, textStatus, errorThrown, options]) if (options.type == 'GET' && textStatus !== 'abort' && allowed) { - locationReplace(container.url) + locationReplace(container.url, options) } } @@ -269,13 +269,13 @@ function pjax(options) { // If there is a layout version mismatch, hard load the new url if (currentVersion && latestVersion && currentVersion !== latestVersion) { - locationReplace(container.url) + locationReplace(container.url, options) return } // If the new response is missing a body, hard load the page if (!container.contents) { - locationReplace(container.url) + locationReplace(container.url, options) return } @@ -394,9 +394,23 @@ function pjaxReload(container, options) { // https://bugs.webkit.org/show_bug.cgi?id=93506 // // Returns nothing. -function locationReplace(url) { - window.history.replaceState(null, "", pjax.state.url) - window.location.replace(url) +function locationReplace(url, options) { + if (options.type.toUpperCase() == 'POST') { + forcePost(options); + } else { + window.history.replaceState(null, "", pjax.state.url) + window.location.replace(url) + } +} + +function forcePost(options) { + var form = options.target; + if (form.nodeName.toUpperCase() != 'FORM') { + return locationReplace(options.url, {}); + } else { + var $form = $(form); + $form.off('submit').submit(); + } } @@ -494,7 +508,7 @@ function onPjaxPopstate(event) { // scroll position. container[0].offsetHeight // eslint-disable-line no-unused-expressions } else { - locationReplace(location.href) + locationReplace(location.href, {}) } } initialPop = false From eb9f8e0a4f4e9a2c0035a44124e35852449bdb3a Mon Sep 17 00:00:00 2001 From: Konstantin Koulechov Date: Wed, 14 Feb 2018 10:29:13 +0100 Subject: [PATCH 2/2] Fixed travis errors --- jquery.pjax.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/jquery.pjax.js b/jquery.pjax.js index 12645879..1364ee8b 100644 --- a/jquery.pjax.js +++ b/jquery.pjax.js @@ -395,8 +395,8 @@ function pjaxReload(container, options) { // // Returns nothing. function locationReplace(url, options) { - if (options.type.toUpperCase() == 'POST') { - forcePost(options); + if (options.type.toUpperCase() === 'POST') { + forcePost(options) } else { window.history.replaceState(null, "", pjax.state.url) window.location.replace(url) @@ -404,12 +404,12 @@ function locationReplace(url, options) { } function forcePost(options) { - var form = options.target; - if (form.nodeName.toUpperCase() != 'FORM') { - return locationReplace(options.url, {}); + var form = options.target + if (form.nodeName.toUpperCase() !== 'FORM') { + return locationReplace(options.url, {}) } else { - var $form = $(form); - $form.off('submit').submit(); + var $form = $(form) + $form.off('submit').submit() } }