jquery control execution of the callback functions

Posted by user253530 on Stack Overflow See other posts from Stack Overflow or by user253530
Published on 2010-04-08T04:34:20Z Indexed on 2010/04/08 5:23 UTC
Read the original article Hit count: 350

Function socialbookmarksTableData(data) is called by another function to generate the content of a table -- data is a JSON object. Inside the function i call 2 other functions that use getJSON and POST (with json as a return object) to get some data. The problem is: though the functions execute correctly i get undefined value for the 2 variables (bookmarkingSites, bookmarkCategories). Help with a solution please.

    function socialbookmarksGetBookmarkCategories(bookmarkID)
    {
        var toReturn = '';
        $.post("php/socialbookmark-get-bookmark-categories.php",{
            bookmarkID: bookmarkID
        },function(data){
            $.each(data,function(i,categID){
                toReturn += '<option value ="' + data[i].categID + '">' + data[i].categName + '</option>';
            })
            return toReturn;
        },"JSON");
    }

    function socialbookmarksGetBookmarkSites()
    {
        var bookmarkingSites = '';
        $.getJSON("php/socialbookmark-get-bookmarking-sites.php",function(bookmarks){

            for(var i = 0; i < bookmarks.length; i++){
                //alert( bookmarks[i].id);
                bookmarkingSites += '<option value = "' + bookmarks[i].id + '">' + bookmarks[i].title + '</option>';
            }
            return bookmarkingSites;
        });
    }
    function socialbookmarksTableData(data)
    {
        var toAppend = '';
        var bookmarkingSites = socialbookmarksGetBookmarkSites();


        $.each(data.results, function(i, id){

            var bookmarkCategories = socialbookmarksGetBookmarkCategories(data.results[i].bookmarkID);
    //rest of the code is not important
        });
    $("#searchTable tbody").append(toAppend);
}

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JavaScript