Created
          September 20, 2012 03:05 
        
      - 
      
 - 
        
Save elijahmanor/3753729 to your computer and use it in GitHub Desktop.  
    AmplifyJS Request Deferreds
  
        
  
    
      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
    
  
  
    
  | function deferredRequest( resource, data ) { | |
| return $.Deferred(function( dfd ) { | |
| amplify.request({ | |
| resourceId: resource, | |
| data: data, | |
| success: dfd.resolve, | |
| error: dfd.reject | |
| }); | |
| }).promise(); | |
| } | |
| deferredRequest( "foo" ).then(function( data ) { | |
| console.log( "dfd", data ); | |
| }); | 
  
    
      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
    
  
  
    
  | var d1 = $.Deferred(), | |
| d2 = $.Deferred(); | |
| amplify.request({ | |
| resourceId: "example1", | |
| success: d1.resolve, | |
| error: d1.reject | |
| }); | |
| amplify.request({ | |
| resourceId: "example2", | |
| success: d2.resolve, | |
| error: d2.reject | |
| }); | |
| $.when(d1, d2).then(function () { | |
| // do something with both | |
| }); | 
  
    
      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
    
  
  
    
  | <!-- Includes core, store, & request components --> | |
| <script src="amplify.min.js"></script> | |
| <!-- Or you may have included each component separately | |
| <script src="amplify.core.min.js"></script> | |
| <script src="amplify.store.min.js"></script> | |
| <script src="amplify.request.min.js"></script> | |
| --> | |
| <!-- Include the deferred plugin after the request component above --> | |
| <script src="amplify.request.deferred.min.js"></script> | 
  
    
      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
    
  
  
    
  | // The following code requires the amplify-request-deferred plugin | |
| $.when( | |
| amplify.request( "getTweets", { userName: "elijahmanor", count: 25 } ), | |
| amplify.request( "getTweets", { userName: "appendto", count: 25 } ) | |
| ).then(function( elijahmanor, appendto ) { | |
| console.log( "data", elijahmanor[ 0 ] ); | |
| console.log( "status", elijahmanor[ 1 ] ); | |
| console.log( "data", appendto[ 0 ] ); | |
| console.log( "status", appendto[ 0 ] ); | |
| }, function( elijahmanor, appendto ) { | |
| // ... | |
| }); | |
| amplify.request( "getTweets", { userName: "elijahmanor", count: 25 } ) | |
| .done(function( data, status ) { | |
| console.log( data ); | |
| console.log( status ); | |
| }).fail(function( data, status ) { | |
| // ... | |
| }); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment