Clone options before modifying it for form submissions

This commit is contained in:
Tim Trinidad
2018-01-22 13:31:11 -05:00
parent b5c2120d08
commit e4b3952589

View File

@@ -6,14 +6,18 @@ var clone = require("../clone")
var attrClick = "data-pjax-click-state" var attrClick = "data-pjax-click-state"
var formAction = function(el, event) { var formAction = function(el, event) {
this.options.requestOptions = { // Since we'll be modifying request options, clone the existing options
// so these changes don't persist
var options = clone(this.options);
options.requestOptions = {
requestUrl: el.getAttribute("action") || window.location.href, requestUrl: el.getAttribute("action") || window.location.href,
requestMethod: el.getAttribute("method") || "GET", requestMethod: el.getAttribute("method") || "GET",
} }
// create a testable virtual link of the form action // create a testable virtual link of the form action
var virtLinkElement = document.createElement("a"); var virtLinkElement = document.createElement("a");
virtLinkElement.setAttribute("href", this.options.requestOptions.requestUrl); virtLinkElement.setAttribute("href", options.requestOptions.requestUrl);
// Ignore external links. // Ignore external links.
if (virtLinkElement.protocol !== window.location.protocol || virtLinkElement.host !== window.location.host) { if (virtLinkElement.protocol !== window.location.protocol || virtLinkElement.host !== window.location.host) {
@@ -34,7 +38,7 @@ var formAction = function(el, event) {
} }
// if declared as a full reload, just normally submit the form // if declared as a full reload, just normally submit the form
if (this.options.currentUrlFullReload) { if (options.currentUrlFullReload) {
el.setAttribute(attrClick, "reload"); el.setAttribute(attrClick, "reload");
return; return;
} }
@@ -56,12 +60,12 @@ var formAction = function(el, event) {
// Creating a getString // Creating a getString
var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value;})).join("&"); var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value;})).join("&");
this.options.requestOptions.requestPayload = paramObject; options.requestOptions.requestPayload = paramObject;
this.options.requestOptions.requestPayloadString = paramsString; options.requestOptions.requestPayloadString = paramsString;
el.setAttribute(attrClick, "submit"); el.setAttribute(attrClick, "submit");
var options = clone(this.options);
options.triggerElement = el; options.triggerElement = el;
this.loadUrl(virtLinkElement.href, options); this.loadUrl(virtLinkElement.href, options);
}; };