Created
November 2, 2011 03:23
-
-
Save eliperelman/1332762 to your computer and use it in GitHub Desktop.
jQuery Deferreds and Promises Demo
This file contains 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> | |
<title>jQuery Deferreds and Promises</title> | |
</head> | |
<body> | |
<div class="container"> | |
<form method="get" action="http://search.twitter.com/search.json"> | |
<input type="search" class="search"> | |
<input type="submit" value="Search teh Tweets!" class="submit"> | |
</form> | |
<div class="tweets"></div> | |
</div> | |
<script src="jquery.min.js"></script> | |
<script src="twitter.js"></script> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains 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 () { | |
var twitterContainer = $( '.tweets' ); | |
$( 'form' ).submit( function ( e ) { | |
var query = $( '.search' ).val(), | |
search = Twitter.search( query ); | |
e.preventDefault(); | |
search.done( function ( response ) { | |
var i = 0, | |
l = response.results.length, | |
container = $( '<div />' ), | |
current; | |
twitterContainer.empty(); | |
for ( ; i < l; i++ ) { | |
current = response.results[ i ]; | |
container | |
.append( '<div class="tweet"><img src="' + current.profile_image_url + '" />' + | |
current.text + '</div>' ); | |
} | |
container.appendTo( twitterContainer ); | |
} ); | |
search.fail( function ( json ) { | |
twitterContainer | |
.html( '<div class="tweet">There was an error trying to retrieve tweets for that search.</div>' ); | |
console.log( json ); | |
} ); | |
search.always( function () { | |
console.log( 'Performed Twitter search for: ' + query ); | |
} ); | |
search.done( function ( response ) { | |
$.ajax( { | |
url: '/store/tweetquery', | |
data: { q: query }, | |
dataType: 'json', | |
type: 'post' | |
} ); | |
} ); | |
}); | |
})(); |
This file contains 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 Twitter = {}; | |
( function ( t, $ ) { | |
var cache = {}; | |
t.search = function ( query ) { | |
return $.Deferred( function ( d ) { | |
if ( cache[ query ] ) { | |
d.resolve( cache[ query ] ); | |
return; | |
} | |
$.ajax( { | |
url: 'http://search.twitter.com/search.json', | |
type: 'get', | |
dataType: 'jsonp', | |
data: { q: query, rpp: 5 } | |
} ) | |
.done( function ( response ) { | |
cache[ query ] = response; | |
if ( response && response.results ) { | |
d.resolve( response ); | |
} else { | |
d.reject( response ); | |
} | |
} ) | |
.fail( function ( xhr, status, error ) { | |
d.reject( JSON.parse( xhr.responseText ) ); | |
} ); | |
} ).promise(); | |
}; | |
} )( Twitter, jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment