Last active
April 17, 2020 02:50
-
-
Save patorash/233fcf64f0337246fddab0ea07ed3c40 to your computer and use it in GitHub Desktop.
Support GET from with Turbolinks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Turbolinks from 'turbolinks'; | |
Turbolinks.start(); | |
document.addEventListener('turbolinks:load', function(event) { | |
for (let form of document.querySelectorAll('form[method=get]:not([data-remote=true])')) { | |
form.addEventListener('submit', function (event) { | |
event.preventDefault(); | |
const entries = [...new FormData(event.target).entries()]; | |
const actionUrl = new URL(event.target.action); | |
const currentUrl = new URL(location.href); | |
// if pathname not changed, hand over per parameter to next page. | |
if (actionUrl.pathname === currentUrl.pathname && currentUrl.searchParams.has('per')) { | |
actionUrl.searchParams.set('per', currentUrl.searchParams.get('per')) | |
} | |
entries.forEach(entry => actionUrl.searchParams.set(...entry)); | |
Turbolinks.visit(actionUrl.toString()); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment