Skip to content

Instantly share code, notes, and snippets.

@bittersweetryan
Created August 27, 2012 17:55
Show Gist options
  • Save bittersweetryan/3490841 to your computer and use it in GitHub Desktop.
Save bittersweetryan/3490841 to your computer and use it in GitHub Desktop.
jQuery Deferrds
$(function(){
var $tweetDiv = $("#tweets>div");
var tweetClickHandler = function(e){
cleanDiv();
getTweets($(this).find('a').data('link'));
$tweetDiv.fadeIn('slow');
}
var cleanDiv = function(){
if($tweetDiv.children().length){
$tweetDiv.children().remove();
}
$tweetDiv.fadeOut('slow');
};
var addTweets = function(tweets){
console.log('add tweets');
$.each(tweets,function(i,tweet){
var $newElem = $('<div><span class="username"></span><span class="tweettext"></span></div>');
$newElem.find("span.username")
.html('<img src="' + tweet.profile_image_url_https + '">')
.end()
.find("span.tweettext")
.html(tweet.text)
.end()
.appendTo($tweetDiv);
});
};
var getTweets = function(searchURL){
console.log('get tweets');
$.ajax({
url : searchURL,
dataType : 'jsonp',
success : function(data){
//make sure what we got back valid data from twitter
if(typeof data === 'object' && ! ('error' in data)){
addTweets(data.results);
}
},
error : function(){
//error
}
});
};
$('li').on('click', $.proxy(tweetClickHandler,this));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment