loadUrl enhancements (#134)

`loadUrl` enhancements

- Make `options` parameter optional
- Allow partial overriding of instance options when calling `loadUrl` directly
- Make `requestOptions` optional
- Document `loadUrl` usage and provide examples
This commit was merged in pull request #134.
This commit is contained in:
Robin North
2018-03-06 10:06:38 +00:00
committed by GitHub
parent f98f2dd63b
commit 05fa833169
15 changed files with 148 additions and 62 deletions

View File

@@ -1,5 +1,5 @@
var on = require("../events/on")
var clone = require("../clone")
var clone = require("../util/clone")
var attrClick = "data-pjax-click-state"

View File

@@ -1,5 +1,5 @@
var on = require("../events/on")
var clone = require("../clone")
var clone = require("../util/clone")
var attrClick = "data-pjax-click-state"
var attrKey = "data-pjax-keyup-state"
@@ -9,9 +9,6 @@ var linkAction = function(el, event) {
// clone it so the changes don't persist
var options = clone(this.options)
// Initialize requestOptions since loadUrl expects it to be an object
options.requestOptions = {}
// Dont break browser special behavior on links (like page in new window)
if (event.which > 1 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
el.setAttribute(attrClick, "modifier")

View File

@@ -1,40 +0,0 @@
/* global _gaq: true, ga: true */
var defaultSwitches = require("../switches")
module.exports = function(options) {
options = options || {}
options.elements = options.elements || "a[href], form[action]"
options.selectors = options.selectors || ["title", ".js-Pjax"]
options.switches = options.switches || {}
options.switchesOptions = options.switchesOptions || {}
options.history = options.history || true
options.analytics = (typeof options.analytics === "function" || options.analytics === false) ?
options.analytics :
function() {
if (window._gaq) {
_gaq.push(["_trackPageview"])
}
if (window.ga) {
ga("send", "pageview", {page: location.pathname, title: document.title})
}
}
options.scrollTo = (typeof options.scrollTo === "undefined") ? 0 : options.scrollTo
options.scrollRestoration = (typeof options.scrollRestoration !== "undefined") ? options.scrollRestoration : true
options.cacheBust = (typeof options.cacheBust === "undefined") ? true : options.cacheBust
options.debug = options.debug || false
options.timeout = options.timeout || 0
options.currentUrlFullReload = (typeof options.currentUrlFullReload === "undefined") ? false : options.currentUrlFullReload
// We cant replace body.outerHTML or head.outerHTML.
// It creates a bug where a new body or head are created in the DOM.
// If you set head.outerHTML, a new body tag is appended, so the DOM has 2 body nodes, and vice versa
if (!options.switches.head) {
options.switches.head = defaultSwitches.switchElementsAlt
}
if (!options.switches.body) {
options.switches.body = defaultSwitches.switchElementsAlt
}
this.options = options
}