Skip to content

Commit 446bc16

Browse files
committed
Merge pull request #484 from sj26/fallback-array-form-data
Cope with array form data in fallback
2 parents 7ce953c + 31840e7 commit 446bc16

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

jquery.pjax.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ function fallbackPjax(options) {
521521
var pair = value.split('=')
522522
form.append($('<input>', {type: 'hidden', name: pair[0], value: pair[1]}))
523523
})
524+
} else if ($.isArray(data)) {
525+
$.each(data, function(index, value) {
526+
form.append($('<input>', {type: 'hidden', name: value.name, value: value.value}))
527+
})
524528
} else if (typeof data === 'object') {
525529
for (key in data)
526530
form.append($('<input>', {type: 'hidden', name: key, value: data[key]}))

test/unit/pjax_fallback.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,54 @@ asyncTest("POST with data object"+s, function() {
313313
})
314314
})
315315

316+
asyncTest("GET with data array"+s, function() {
317+
var frame = this.frame
318+
319+
this.loaded = function() {
320+
equal(frame.location.pathname, "/env.html")
321+
equal(frame.location.search, "?foo%5B%5D=bar&foo%5B%5D=baz")
322+
323+
var env = JSON.parse(frame.$("#env").text())
324+
equal(env['REQUEST_METHOD'], "GET")
325+
var expected = {'foo': ['bar', 'baz']};
326+
if (!disabled) expected._pjax = "#main"
327+
deepEqual(env['rack.request.query_hash'], expected)
328+
329+
start()
330+
}
331+
332+
frame.$.pjax({
333+
type: 'GET',
334+
url: "env.html",
335+
data: [{name: "foo[]", value: "bar"}, {name: "foo[]", value: "baz"}],
336+
container: "#main"
337+
})
338+
})
339+
340+
asyncTest("POST with data array"+s, function() {
341+
var frame = this.frame
342+
343+
this.loaded = function() {
344+
equal(frame.location.pathname, "/env.html")
345+
equal(frame.location.search, "")
346+
347+
var env = JSON.parse(frame.$("#env").text())
348+
equal(env['REQUEST_METHOD'], "POST")
349+
var expected = {'foo': ['bar', 'baz']};
350+
if (!disabled) expected._pjax = "#main"
351+
deepEqual(env['rack.request.form_hash'], expected)
352+
353+
start()
354+
}
355+
356+
frame.$.pjax({
357+
type: 'POST',
358+
url: "env.html",
359+
data: [{name: "foo[]", value: "bar"}, {name: "foo[]", value: "baz"}],
360+
container: "#main"
361+
})
362+
})
363+
316364
asyncTest("GET with data string"+s, function() {
317365
var frame = this.frame
318366

0 commit comments

Comments
 (0)