Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try submit form instead of go to action URL when POST on failure #696

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions jquery.pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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()
}
}


Expand Down Expand Up @@ -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
Expand Down