Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the same options object in handle-response as in send-request #148

Merged
merged 3 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ declare class Pjax {
* @param {string} requestText The raw text of the response. Same as <code>request.responseText</code>.
* @param {XMLHttpRequest} request The XHR object.
* @param {string} href The original URI used to initiate the request.
* @param options The Pjax options object used for the request
*/
handleResponse(requestText: string, request: XMLHttpRequest, href: string): void;
handleResponse(requestText: string, request: XMLHttpRequest, href: string, options?: Pjax.IOptions): void;

/**
* Initiates the request by calling <code>doRequest()</code>.
Expand Down
3 changes: 2 additions & 1 deletion lib/proto/attach-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ var formAction = function(el, event) {
var tagName = element.tagName.toLowerCase()
// jscs:disable disallowImplicitTypeConversion
if (!!element.name && element.attributes !== undefined && tagName !== "button") {
var type = element.attributes.type
// jscs:enable disallowImplicitTypeConversion
var type = element.attributes.type

if ((!type || type.value !== "checkbox" && type.value !== "radio") || element.checked) {
// Build array of values to submit
var values = []
Expand Down
12 changes: 6 additions & 6 deletions lib/proto/handle-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ var clone = require("../util/clone.js")
var newUid = require("../uniqueid.js")
var trigger = require("../events/trigger.js")

module.exports = function(responseText, request, href) {
var tempOptions = clone(this.options);
tempOptions.request = request
module.exports = function(responseText, request, href, options) {
options = clone(options || this.options)
options.request = request

// Fail if unable to load HTML via AJAX
if (responseText === false) {
trigger(document, "pjax:complete pjax:error", tempOptions)
trigger(document, "pjax:complete pjax:error", options)

return
}
Expand Down Expand Up @@ -49,13 +49,13 @@ module.exports = function(responseText, request, href) {
}

this.state.href = href
this.state.options = clone(this.options)
this.state.options = options

try {
this.loadContent(responseText, this.options)
}
catch (e) {
trigger(document, "pjax:error", tempOptions)
trigger(document, "pjax:error", options)

if (!this.options.debug) {
if (console && console.error) {
Expand Down
8 changes: 4 additions & 4 deletions lib/send-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ module.exports = function(location, options, callback) {
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
callback(request.responseText, request, location)
callback(request.responseText, request, location, options)
}
else if (request.status !== 0) {
callback(null, request, location)
callback(null, request, location, options)
}
}
}

request.onerror = function(e) {
console.log(e)
callback(null, request, location)
callback(null, request, location, options)
}

request.ontimeout = function() {
callback(null, request, location)
callback(null, request, location, options)
}

// Prepare the request payload for forms, if available
Expand Down
8 changes: 6 additions & 2 deletions tests/lib/proto/handle-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ tape("test events triggered when handleResponse(false) is called", function(t) {

tape("test when handleResponse() is called normally", function(t) {
var pjax = {
options: {},
options: {
test: 1
},
loadContent: noop,
state: {}
}
Expand All @@ -55,7 +57,9 @@ tape("test when handleResponse() is called normally", function(t) {
scrollPos: [0, 0]
}, "window.history.state is set correctly")
t.equals(pjax.state.href, "href", "this.state.href is set correctly")
t.same(pjax.state.options, pjax.options, "this.state.options is set correctly")
t.equals(Object.keys(pjax.state.options).length, 2, "this.state.options is set correctly")
t.same(pjax.state.options.request, request, "this.state.options is set correctly")
t.equals(pjax.state.options.test, 1, "this.state.options is set correctly")

t.end()
})
Expand Down