Created
August 27, 2012 14:15
-
-
Save bittersweetryan/3488815 to your computer and use it in GitHub Desktop.
jquery deferrds
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(){ | |
var $tweetDiv = $("#tweets>div"); | |
$('li').on('click',function(){ | |
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 | |
} | |
}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment