From 6c7758661689ac9cd315329e15fa9415dd3e8d6b Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Sun, 25 Jan 2015 22:49:46 -0500 Subject: [PATCH] #426 make array-to-querystring serialization work like jquery --- docs/change-log.md | 1 + mithril.js | 9 ++++++++- tests/mithril-tests.js | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/change-log.md b/docs/change-log.md index 3ad284ed..6424e92f 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -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) --- diff --git a/mithril.js b/mithril.js index bfdcc2f6..f1b4df17 100644 --- a/mithril.js +++ b/mithril.js @@ -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++) { diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 9fa41460..cbf52d4d 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -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(){