Skip to content

Commit

Permalink
Support functions as values in $.param()
Browse files Browse the repository at this point in the history
Each function value will be called to receive the final value to be serialized.
  • Loading branch information
mislav committed Dec 11, 2014
1 parent e5f6851 commit 1e999d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
7 changes: 7 additions & 0 deletions test/ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,13 @@ <h1>Zepto Ajax unit tests</h1>
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)
Expand Down

0 comments on commit 1e999d6

Please sign in to comment.