0

Ok I've tried everything. I'm reading up on async calls and callback methods and promises and deferred vars, I can't seem to wrap my head around how to fix my problem even though I'm really really trying to learn this. I have an api that i need to use to get a list of objects, the part of the api that is of concern is such:

/** Retrieve a list of data on the server.
     * HTTP GET to the given {@code uri} presumes that the result is XHTML unordered list of HREFs.
     * @param path (if null, default to /data) 
     * @returns callback(files[]) with file urls. */
    list: function(path, callback) {
        jquery.get(path, function(response) {

            var list = [];
            function() {//not the actual code, but it basically creates a data object and pushes it to the array 'list'
                var data = {};                      
                data.blah = blah;
                });
                list.push(data);
            });
            callback(list);
        });
    },

how can I use the abode api to store the list in a variable outside of where I call it? My code is below:

$("#data-tab").click(function(){

        var url = "blah.com";

        var tree = [];

        api.File.list(url, function(data){
              tree = data;
        });

        for(item in tree){//tested with this
            console.log(item.Name);//this doesnt work because tree is not populated with objects
        }
};

Now i know i can do something with the data by writing something like:

$.each(list, function(i, item){
    doSomething(item);
});

but if i do, i cant use it to push the objects into the array and i need to be able to access this array of objects outside of this scope. Below code is not working:

        var tree = [];
        var idDictionary = new Object();

        function addNodes(path, dictionary, object){

        if (path==null || path.length==0 || dictionary[path] != null){
            return;
        }
        var node = {
            id : path,
            text: path.substring(path.lastIndexOf('/') + 1, path.length),
            parent: path.substring(0, path.lastIndexOf('/'))
        };


        dictionary[path] = node;

        addNodes(node.parent, dictionary, null);

        };

        api.File.list(url, function(list) {
            $.each(list, function(i,object) {

                addNodes(object.id, idDictionary, object);

                });
            }); 
       for(node in idDictionary){
          tree.push(idDictionary[node]);
       }

Nothing gets stored to the dictionary or tree. How can I improve my code so it works?

dalva
  • 55
  • 7
  • See http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call – elclanrs Mar 31 '15 at 03:20
  • @elclanrs I have read that but I'm having difficulty applying it to my particular case. Still pretty new to Javascript – dalva Mar 31 '15 at 03:23
  • `jQuery.get(path).then(callback)` ? `api.File.list(url, function(data){ tree = data; for(item in tree){ console.log(item.Name); } });` ? – guest271314 Mar 31 '15 at 03:26
  • 2
    Simple answer: Put everything that depends on the async call in the callback. – Barmar Mar 31 '15 at 03:28
  • Try substituting uppercase `Q` at `jQuery.get(path)` http://api.jquery.com/jQuery.get – guest271314 Mar 31 '15 at 03:33
  • @guest271314 no that is part of the api, the first sample code is just part of the api that i need to use and is working just fine. In my second sample code you see where i call it with api.File.list – dalva Mar 31 '15 at 03:36

0 Answers0