Created
July 25, 2011 13:23
-
-
Save scottgonzalez/1104101 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Multiple Requests</title> | |
| <script src="jquery-1.6.1.js"></script> | |
| <script src="amplify.core.js"></script> | |
| <script src="amplify.request.js"></script> | |
| <script> | |
| var multiRequest = function( requests ) { | |
| var pending = {}, | |
| ret = {}, | |
| successCallbacks = [], | |
| errorCallbacks = []; | |
| $.each( requests, function( name ) { | |
| pending[ name ] = amplify.request( $.extend( this, { | |
| success: function( data ) { | |
| successHandler( name, data ); | |
| }, | |
| error: errorHandler | |
| })); | |
| }); | |
| function successHandler( resourceId, data ) { | |
| ret[ resourceId ] = data; | |
| delete pending[ resourceId ]; | |
| for ( var waiting in pending ) { | |
| break; | |
| } | |
| if ( !waiting ) { | |
| done(); | |
| } | |
| } | |
| function done() { | |
| $.each( successCallbacks, function() { | |
| this( ret ); | |
| }); | |
| } | |
| function errorHandler() { | |
| errorHandler = $.noop; | |
| $.each( pending, function() { | |
| this.abort(); | |
| }); | |
| $.each( errorCallbacks, function() { | |
| this(); | |
| }); | |
| } | |
| return { | |
| abort: errorHandler, | |
| success: function( fn ) { | |
| successCallbacks.push( fn ); | |
| return this; | |
| }, | |
| error: function( fn ) { | |
| errorCallbacks.push( fn ); | |
| return this; | |
| } | |
| }; | |
| }; | |
| </script> | |
| <script> | |
| amplify.request.define( "foo", "ajax", { | |
| url: "data/foo.json", | |
| dataType: "json" | |
| }); | |
| amplify.request.define( "bar", "ajax", { | |
| url: "data/bar.json", | |
| dataType: "json" | |
| }); | |
| </script> | |
| <script> | |
| multiRequest({ | |
| f: { resourceId: "foo" }, | |
| b: { resourceId: "bar", data: { baz: "qux" } } | |
| }) | |
| .success(function( data ) { | |
| console.log( data ); | |
| }) | |
| .error(function() { | |
| // uhoh | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment