jquery - Deferred object not containing exact values in json object -
i creating 1 javascript library. in using deferred methods asynchronous call. thing is, if log output json object not showing items. if expand object showing actual values. below screenshot.
in console.log
showing subfolders object contains 0 elements, if expand object subfolders containing 2 elements. output object coming multiple asynchronous calls. following code
restqueries.getfilesfromfolders = function(){ var execute = function(libraryname){ var libraryitems={rootfolder:[],subfolders:[]}; var deferred = $.deferred(); var _url = makeproperurl(); var itemsinrootfolder = getitemsfromfolder(libraryname,'files'); itemsinrootfolder.then(function(data){ var temparray = []; if(typeof(data.d.results)!='undefined' && data.d.results.length>0) { for(var =0;i<data.d.results.length;i++) { temparray.push({"name":data.d.results[i].name}); } } libraryitems.rootfolder.push(temparray); }).then(function(){ var subfolders = getitemsfromfolder(libraryname,'folders'); subfolders.then(function(data){ for(var item in data.d.results){ var fname = data.d.results[item].name; if(fname!='forms') { var subfolderitems = getitemsfromfolder(libraryname+"/"+fname ,'files'); subfolderitems.then(function(subitems,link){ var actualfoldername = link.split("/")[1]; var temparray={}; var temparray2 = []; for(var i=0;i<subitems.d.results.length;i++){ temparray2.push({"name":subitems.d.results[i].name}); } temparray[actualfoldername]=temparray2; temparray2=""; libraryitems.subfolders.push(temparray); }); } } }); }).then(function(){ deferred.resolve(libraryitems); }); return deferred; }; return{ execute:execute } }();
i executing function like,
restqueries.getfilesfromfolders.execute('helpdocuments').then(function(data){ console.log(data); }, function(err){ console.log(err); });
why json object behaving this? doing mistake?
check if code work
restqueries.getfilesfromfolders = function(){ var execute = function(libraryname){ var libraryitems={rootfolder:[],subfolders:[]}; var deferred = $.deferred(); var _url = makeproperurl(); var itemsinrootfolder = getitemsfromfolder(libraryname,'files'); itemsinrootfolder.then(function(data){ var temparray = []; if(typeof(data.d.results)!='undefined' && data.d.results.length>0) { for(var =0;i<data.d.results.length;i++) { temparray.push({"name":data.d.results[i].name}); } } libraryitems.rootfolder.push(temparray); var subfolders = getitemsfromfolder(libraryname,'folders'); subfolders.then(function(data){ for(var item in data.d.results){ var fname = data.d.results[item].name; if(fname!='forms') { var subfolderitems = getitemsfromfolder(libraryname+"/"+fname ,'files'); subfolderitems.then(function(subitems,link){ var actualfoldername = link.split("/")[1]; var temparray={}; var temparray2 = []; for(var i=0;i<subitems.d.results.length;i++){ temparray2.push({"name":subitems.d.results[i].name}); } temparray[actualfoldername]=temparray2; temparray2=""; libraryitems.subfolders.push(temparray); deferred.resolve(libraryitems); }); } } }); }); return deferred; }; return{ execute:execute } }();
Comments
Post a Comment