#426 make array-to-querystring serialization work like jquery

This commit is contained in:
Leo Horie 2015-01-25 22:49:46 -05:00
parent 028425ca08
commit 6c77586616
3 changed files with 14 additions and 1 deletions

View file

@ -9,6 +9,7 @@
### Bug Fixes:
- prevent empty class attributes [#382](https://github.com/lhorie/mithril.js/issues/382)
- array-to-querystring serialization in `m.request` now behaves like jQuery [#426](https://github.com/lhorie/mithril.js/issues/426)
---

View file

@ -684,10 +684,17 @@ var m = (function app(window, undefined) {
var str = [];
for(var prop in object) {
var key = prefix ? prefix + "[" + prop + "]" : prop, value = object[prop];
str.push(value != null && type.call(value) === OBJECT ? buildQueryString(value, key) : encodeURIComponent(key) + "=" + encodeURIComponent(value))
var valueType = type.call(value)
var pair = value != null && (valueType === OBJECT) ?
buildQueryString(value, key) :
valueType === ARRAY ?
value.map(function(item) {return encodeURIComponent(key) + "=" + encodeURIComponent(item)}).join("&") :
encodeURIComponent(key) + "=" + encodeURIComponent(value)
str.push(pair)
}
return str.join("&")
}
function parseQueryString(str) {
var pairs = str.split("&"), params = {};
for (var i = 0, len = pairs.length; i < len; i++) {

View file

@ -1781,6 +1781,11 @@ function testMithril(mock) {
mock.XMLHttpRequest.$instances.pop().onreadystatechange()
return prop().url === "test"
})
test(function() {
var prop = m.request({method: "GET", url: "test", data: {foo: [1, 2]}})
mock.XMLHttpRequest.$instances.pop().onreadystatechange()
return prop().url === "test?foo=1&foo=2"
})
// m.request over jsonp
test(function(){