This script is meant to look for an id in the data attribute in each containment div, then send an Ajax call to get the amount of retweets, calculate the karma based on those retweets and then display that number in a div.
This code looks like spaghetti. How do I improve this to make each function more independent? There is some abstraction function that I can't figure out.
My other question is, how can I use a jQuery deferred object to create a promise and update the div once I get a response?
function get_retweet(id) {
var url = 'https://api.twitter.com/1/statuses/show/' + id + '.json',
karma;
$.ajax({
url: url,
type: 'GET',
dataType:'jsonp',
crossDomain: true,
success: function(data) {
display_karma(data.retweet_count);
},
error: function() {alert('fail');}
}));
}
function calc_karma(tweets) {
return tweets *10 +10;
}
function display_karma(retweets) {
var id, karma,
el =$('.tweetContainer');
//id = $(el).data(el, tweet_id);
id = '248988915661410304';
karma = calc_karma(retweets);
el.find('.tcPoints').text(karma);
}
function start_get_karma() {
var id;
$('.tweetContainer').each(function(index,el) {
//id = $(el).data(el, tweet_id);
id = '248988915661410304';
get_retweet(id);
});
}