Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem keeping ajax data persistent to store into array

    I am retrieving a list of cloudcasts from mixcloud, storing them into 3 different arrays categorized by music genre. I then attempt to do another ajax call to get the embed code needed for each one and store those into the same multi-dimensional array. These calls are all performed on mixcloud's api.

    The purpose I am trying to achieve is to store the data into 3 categories, then when a user clicks on the relevant selection it will re-populate a div from the appropriate array. I am not able to store the data into an array that keeps the ajax data outside of the calling ajax function however. Seems this is a callback problem, but not able to wrap my head around it.

    Here is the relevant code:
    Code:
        function requestCloudcastsEmbeded(){
            $.ajax({
                'async' : false,
                'url' : 'http://api.mixcloud.com/'+mixcloud.settings.username+'/cloudcasts/?limit=100',
                //'success' : receiveCloudcastsEmbeded
                'success' : function(response){
                    mixcloud_data = response;
    
                    var cloudcast_amount = mixcloud_data.data.length;
    
                    if(cloudcast_amount == 0){
                        $('#music-page #content .container .box #albums .hide').html('<div style="border: 1px solid red">Mixcloud.com user '+mixcloud.settings.username+' hasn\'t uploaded any sets!</div>');
                        return false;
                    }
    
                    $.each(mixcloud_data.data, function(i, data) {
                        var found_type = 0;
                        for (var c = 0; c < data.tags.length; c++) {
                            if (found_type == 0) {
                                if (data.tags[c].name == "Club") {
                                    g_mixcloud_club[club_array_count] = [];
                                    g_mixcloud_club[club_array_count][0] = data.name;
                                    g_mixcloud_club[club_array_count][1] = data.key;
                                    club_array_count++;
                                    found_type = 1;
                                }
                                else if (data.tags[c].name == "Deep house") {
                                    g_mixcloud_house[house_array_count] = [];
                                    g_mixcloud_house[house_array_count][0] = data.name;
                                    g_mixcloud_house[house_array_count][1] = data.key;
                                    house_array_count++;
                                    found_type = 2;
                                }
                            }
                        }
                        if (found_type == 0) {
                            g_mixcloud_other[other_array_count] = [];
                            g_mixcloud_other[other_array_count][0] = data.name;
                            g_mixcloud_other[other_array_count][1] = data.key;
                            other_array_count++;
                        }
                    });
    
                    var string;
                    for(var c=0;c<g_mixcloud_club.length;c++) {
                        $.ajax({
                            'async': false,
                            'url': 'http://api.mixcloud.com' + g_mixcloud_club[c][1] + 'embed-json/',
                            'success': function (data) {
                                string = data;
                                string = string.html;
                                if (mixcloud.settings.width) string = string.replace(/ width="\d+"/g, ' width="' + mixcloud.settings.width + '"');
                                if (mixcloud.settings.height) string = string.replace(/ height="\d+"/g, ' height="' + mixcloud.settings.height + '"');
    
                                g_mixcloud_club[c][2] = string;
                            }
                        });
                    }
                    for(var c=0;c<g_mixcloud_house.length;c++) {
                        $.ajax({
                            'async': false,
                            'url': 'http://api.mixcloud.com' + g_mixcloud_house[c][1] + 'embed-json/',
                            'success': function (data) {
                                string = data;
                                string = string.html;
                                if (mixcloud.settings.width) string = string.replace(/ width="\d+"/g, ' width="' + mixcloud.settings.width + '"');
                                if (mixcloud.settings.height) string = string.replace(/ height="\d+"/g, ' height="' + mixcloud.settings.height + '"');
    
                                 g_mixcloud_house[c][2] = string;
                            }
                        });
                    }
                    for(var c=0;c<g_mixcloud_other.length;c++) {
                        $.ajax({
                            'async': false,
                            'url': 'http://api.mixcloud.com' + g_mixcloud_other[c][1] + 'embed-json/',
                            'success': function (data) {
                                string = data;
                                string = string.html;
                                if (mixcloud.settings.width) string = string.replace(/ width="\d+"/g, ' width="' + mixcloud.settings.width + '"');
                                if (mixcloud.settings.height) string = string.replace(/ height="\d+"/g, ' height="' + mixcloud.settings.height + '"');
    
                                g_mixcloud_other[c][2] = string;
                            }
                        });
                    }
                }
            });
        }
    The last 3 for loops trying to pull in the embed ajax data is only pulling in the very last cloudcast data and applies it only to 1 out of them all.

    For the time being I changed it where it just auto populates 3 div's for each genre and does a show/hide on them, but this is having problems of it's own and really wish I could get the arrays to work and just have 1 div I can populate on the fly when needed.

    Thanks for the time in reading this, and any help.

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Without taking too much time (I'm on break), I feel like we're missing part of your code. I see where you're iterating variables like club_array_count and house_array_count, but you never initialize them to a value (0 or 1, I'd presume is your start value?).


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •