From 1e999d669da1ebb4cc3fecc3e5205dbacf50a9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 11 Dec 2014 06:11:48 -0800 Subject: [PATCH] Support functions as values in `$.param()` Each function value will be called to receive the final value to be serialized. --- src/ajax.js | 1 + test/ajax.html | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/ajax.js b/src/ajax.js index d24ab5915..cbb49d8f3 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -349,6 +349,7 @@ $.param = function(obj, traditional){ var params = [] params.add = function(key, value) { + if ($.isFunction(value)) value = value() if (value == null) value = "" this.push(escape(key) + '=' + escape(value)) } diff --git a/test/ajax.html b/test/ajax.html index 70ff2ff9c..953d235f1 100644 --- a/test/ajax.html +++ b/test/ajax.html @@ -1450,6 +1450,13 @@

Zepto Ajax unit tests

decodeURIComponent($.param([{ name: "x", value: null }]))) }, + testParamFunctionValues: function(t) { + t.assertEqual("x=&a[b]=B", + decodeURIComponent($.param({ x: function(){}, a: { b: function(){ return "B" }}}))) + t.assertEqual("x=&a[b]=B", + decodeURIComponent($.param([{ name: "x", value: null }, { name: "a[b]", value: function(){ return "B" }}]))) + }, + testParamEscaping: function(t) { var result = $.param({ 'equation[1]': 'bananas+peaches=smoothie' }) t.assertEqual("equation%5B1%5D=bananas%2Bpeaches%3Dsmoothie", result)