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?