From e4b395258904f24ffa1b82fa56c8cd9d3846e4f9 Mon Sep 17 00:00:00 2001 From: Tim Trinidad Date: Mon, 22 Jan 2018 13:31:11 -0500 Subject: [PATCH] Clone options before modifying it for form submissions --- lib/proto/attach-form.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/proto/attach-form.js b/lib/proto/attach-form.js index 96bf39d..1a0ca83 100644 --- a/lib/proto/attach-form.js +++ b/lib/proto/attach-form.js @@ -6,14 +6,18 @@ var clone = require("../clone") var attrClick = "data-pjax-click-state" 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, requestMethod: el.getAttribute("method") || "GET", } // create a testable virtual link of the form action var virtLinkElement = document.createElement("a"); - virtLinkElement.setAttribute("href", this.options.requestOptions.requestUrl); + virtLinkElement.setAttribute("href", options.requestOptions.requestUrl); // Ignore external links. 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 (this.options.currentUrlFullReload) { + if (options.currentUrlFullReload) { el.setAttribute(attrClick, "reload"); return; } @@ -56,12 +60,12 @@ var formAction = function(el, event) { // Creating a getString var paramsString = (paramObject.map(function(value) {return value.name + "=" + value.value;})).join("&"); - this.options.requestOptions.requestPayload = paramObject; - this.options.requestOptions.requestPayloadString = paramsString; + options.requestOptions.requestPayload = paramObject; + options.requestOptions.requestPayloadString = paramsString; el.setAttribute(attrClick, "submit"); - var options = clone(this.options); + options.triggerElement = el; this.loadUrl(virtLinkElement.href, options); };