Skip to main content
added 188 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

If I write spaghetti code, I'm gonna have a bad time. Help with this ajax/jquery logic please :) Calculate karma based on retweets

This is a pretty simple script.

In short it's is meant to look for an id in the data attribute in each containment div, then send an ajaxAjax call to get the amount of retweets, calculate the karma based on those retweets and then display that number in a div.

This code is prettylooks like spaghetti. How do I improve this to make each function more independent. (Some? There is some abstraction function that I can't figure out).

My other question is, how can I use a jqueryjQuery deferred object to create a promise and update the div once iI 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);
    });
}

If I write spaghetti code, I'm gonna have a bad time. Help with this ajax/jquery logic please :)

This is a pretty simple script.

In short it's 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 is pretty spaghetti. How do I improve this to make each function more independent. (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);
    });
}

Calculate karma based on retweets

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);
    });
}
Tweeted twitter.com/#!/StackCodeReview/status/250203359338975232
edited title
Link

If I write spaghetti code, I'm gonna have a bad time. Help with this ajax/jquery logic please :)

Source Link

If I write spaghetti code, I'm gonna have a bad time. Help with this ajax/jquery logic

This is a pretty simple script.

In short it's 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 is pretty spaghetti. How do I improve this to make each function more independent. (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);
    });
}