Permalink
Browse files

Merge pull request #7 from rboyce/master

Support predefined callback.

This is awesome -- thank you!
  • Loading branch information...
2 parents 6893a7f + fbfddc7 commit 7eeb2f85bda672f51b4ee8c900475fda73355ac1 @dandean committed Jun 6, 2011
Showing with 25 additions and 2 deletions.
  1. +13 −0 README.md
  2. +12 −2 jsonp.js
View
@@ -105,6 +105,19 @@ new Ajax.JSONRequest('http://api.flickr.com/services/feeds/photos_public.gne', {
});
</pre>
+Making signed requests
+----------------------
+
+To make a signed request (such as an OAuth request), it is generally necessary to
+fix the callback parameter before generating the request signature. To support this,
+if the property corresponding to `callbackParamName` is defined in the `parameters`
+object, that callback will be used instead of an automatically generated one.
+
+When providing your own callback parameter, keep the asynchronous aspect of these
+requests in mind. It is possible for two requests with the same callback parameter
+to overwrite each other; therefore, it is recommended that you make these callback
+parameters unique for any requests that may occur concurrently.
+
Credits
=======
View
14 jsonp.js 100644 → 100755
@@ -4,13 +4,14 @@
* Might as well just call this an iteration.
*
* This version introduces:
+ * - Support for predefined callbacks (Necessary for OAuth signed requests, by @rboyce)
* - Partial integration with Ajax.Responders (Thanks to @sr3d for the kick in this direction)
* - Compatibility with Prototype 1.7 (Thanks to @soung3 for the bug report)
* - Will not break if page lacks a <head> element
*
* See examples in README for usage
*
- * VERSION 1.1.1
+ * VERSION 1.1.2
*
* new Ajax.JSONRequest(url, options);
* - url (String): JSON-P endpoint url.
@@ -67,7 +68,16 @@ Ajax.JSONRequest = Class.create(Ajax.Base, (function() {
Ajax.Responders.dispatch('onComplete', this, response);
}.bind(this);
- // Add callback as a parameter and build request URL
+ // If the callback parameter is already defined, use that
+ if (this.options.parameters[key] !== undefined) {
+ name = this.options.parameters[key];
+ }
+ // Otherwise, add callback as a parameter
+ else {
+ this.options.parameters[key] = name;
+ }
+
+ // Build request URL
this.options.parameters[key] = name;
var url = this.options.url + ((this.options.url.include('?') ? '&' : '?') + Object.toQueryString(this.options.parameters));

0 comments on commit 7eeb2f8

Please sign in to comment.