Instead of cloning this.options again in handle-response.js, pass the options object from send-request.js. This way, pjax.state.options will also have the request options.
71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
var clone = require("../util/clone.js")
|
|
var newUid = require("../uniqueid.js")
|
|
var trigger = require("../events/trigger.js")
|
|
|
|
module.exports = function(responseText, request, href, options) {
|
|
options = options || clone(this.options);
|
|
options.request = request
|
|
|
|
// Fail if unable to load HTML via AJAX
|
|
if (responseText === false) {
|
|
trigger(document, "pjax:complete pjax:error", options)
|
|
|
|
return
|
|
}
|
|
|
|
// push scroll position to history
|
|
var currentState = window.history.state || {}
|
|
window.history.replaceState({
|
|
url: currentState.url || window.location.href,
|
|
title: currentState.title || document.title,
|
|
uid: currentState.uid || newUid(),
|
|
scrollPos: [document.documentElement.scrollLeft || document.body.scrollLeft,
|
|
document.documentElement.scrollTop || document.body.scrollTop]
|
|
},
|
|
document.title, window.location)
|
|
|
|
// Check for redirects
|
|
var oldHref = href
|
|
if (request.responseURL) {
|
|
if (href !== request.responseURL) {
|
|
href = request.responseURL
|
|
}
|
|
}
|
|
else if (request.getResponseHeader("X-PJAX-URL")) {
|
|
href = request.getResponseHeader("X-PJAX-URL")
|
|
}
|
|
else if (request.getResponseHeader("X-XHR-Redirected-To")) {
|
|
href = request.getResponseHeader("X-XHR-Redirected-To")
|
|
}
|
|
|
|
// Add back the hash if it was removed
|
|
var a = document.createElement("a")
|
|
a.href = oldHref
|
|
var oldHash = a.hash
|
|
a.href = href
|
|
if (oldHash && !a.hash) {
|
|
a.hash = oldHash
|
|
href = a.href
|
|
}
|
|
|
|
this.state.href = href
|
|
this.state.options = options
|
|
|
|
try {
|
|
this.loadContent(responseText, this.options)
|
|
}
|
|
catch (e) {
|
|
trigger(document, "pjax:error", options)
|
|
|
|
if (!this.options.debug) {
|
|
if (console && console.error) {
|
|
console.error("Pjax switch fail: ", e)
|
|
}
|
|
return this.latestChance(href)
|
|
}
|
|
else {
|
|
throw e
|
|
}
|
|
}
|
|
}
|