Permalink
Browse files

Fix for `jQuery.ajax` receiving a non-string `data` option.

  • Loading branch information...
1 parent 5eb661f commit 681b353d11c91cd682fb6ed96cac6c0cfd8aeb06 @djanowski djanowski committed Jan 31, 2011
Showing with 32 additions and 1 deletion.
  1. +3 −0 CHANGELOG.md
  2. +23 −0 spec/js-libs-compat.coffee
  3. +6 −1 src/zombie/resources.coffee
View
3 CHANGELOG.md
@@ -13,6 +13,9 @@ to `npm install base64` (Assaf Arkin).
Tested with Node 0.3.7 (Assaf Arkin).
+Fix for `jQuery.ajax` receiving a non-string `data` option (Damian
+Janowski).
+
283 Tests
4.6 sec to complete
View
23 spec/js-libs-compat.coffee
@@ -15,6 +15,10 @@ brains.get "/jquery", (req, res)-> res.send """
</select>
<span id="option"></span>
+
+ <a href="#post">Post</a>
+
+ <div id="response"></div>
</body>
<script>
@@ -24,11 +28,24 @@ brains.get "/jquery", (req, res)-> res.send """
$("#option").text(this.value);
});
+ $("a[href='#post']").click(function() {
+ $.post("/echo", {"foo": "bar"}, function(response) {
+ $("#response").text(response);
+ });
+
+ return false;
+ });
});
</script>
</html>
"""
+brains.post "/echo", (req, res)->
+ lines = for key, value of req.body
+ key + "=" + value
+
+ res.send lines.join("\n")
+
vows.describe("Compatibility with JavaScript libraries").addBatch(
"jQuery":
zombie.wants "http://localhost:3003/jquery"
@@ -38,5 +55,11 @@ vows.describe("Compatibility with JavaScript libraries").addBatch(
@callback null, browser
"should fire the change event": (browser)-> assert.equal browser.text("#option"), "1"
+ "jQuery.post":
+ topic: (browser)->
+ browser.clickLink "Post", @callback
+ "should perform an AJAX POST request": (browser)->
+ assert.match browser.text("#response"), /foo=bar/
+
).export(module)
View
7 src/zombie/resources.coffee
@@ -260,14 +260,19 @@ class Resources extends Array
callback error
request.end body, "utf8"
+ typeOf = (object)->
+ return Object.prototype.toString.call(object)
+
# We use this to convert data array/hash into application/x-www-form-urlencoded
stringifyPrimitive = (v) =>
- switch Object.prototype.toString.call(v)
+ switch typeOf(v)
when '[object Boolean]' then v ? 'true' : 'false'
when '[object Number]' then isFinite(v) ? v : ''
when '[object String]' then v
else ''
+
stringify = (object) =>
+ return object if typeOf(object) == '[object String]'
object.map((k) ->
if Array.isArray(k[1])
k[1].map((v) ->

0 comments on commit 681b353

Please sign in to comment.