javascript - Creating nested folder with CordovFile dependency -


i have create, nested folder on android mobile, ionic framework. problem asynchronous request, before finishing first folder goes below code create second folder inside first folder, folder not created yet, gives error.

i searched can worked out angular $q service, tried not working, don't know how work, below code.

controller

if( $scope.projectdetails.clientid == null && $scope.projectdetails.locationid == null ){             // insert row in client table             var parameters=[$scope.projectdetails.clientname];             newproject.insertclient(parameters).then(function(result){                $scope.projectdetails.clientid = result.insertid;                // create folder name in renewate folder                projectcreated.createfolder("renewate",$scope.projectdetails.clientname).then(function(resolve){                 // create new location                 var parameters=[$scope.projectdetails.clientid, $scope.projectdetails.locationname];                 newproject.insertlocation(parameters).then(function(result){                     $scope.projectdetails.locationid = result.insertid;                     // create folder name in renewate folder                     var folderpath = "renewate"+"/"+$scope.projectdetails.clientname;                     projectcreated.createfolder(folderpath, $scope.projectdetails.locationname).then(function(resolve){                           var parameters=[$scope.projectdetails.projectname, $scope.projectdetails.locationid];                           newproject.insertproject(parameters).then(function(result){                               $scope.projectdetails.projectid = result.insertid;                               var folderpath = "renewate"+"/"+$scope.projectdetails.clientname+"/"+$scope.projectdetails.locationname;                               projectcreated.createfolder(folderpath, $scope.projectdetails.projectname).then(function(resolve){                                    //$state.go('renewate3');                               });                           });                     });                  });             });           });         } 

service

createfolder: function(folderpath, foldername){           var q = $q.defer();           $ionicplatform.ready(function () {               $cordovafile.createdir(cordova.file.externalrootdirectory+folderpath, foldername, true)               .then(function (success) {                 q.resolve(result);               },               function (error) {                   q.reject(error);                   alert("error");               });           });           return q.promise;         }, 

please let me know mistaken.

your code should work, i'm not sure if stated there error happening in this actual version of code. if error happens, it's hard spot since it's messy.

rewriting service

looking @ createfolder function in service, you've implemented called deferred anti-pattern.

the $ionicplatform.ready returns promise (see documentation) there no need create new promise you're doing right now. should instead return existing promise like

return $ionicplatform.ready(function () {     return $cordovafile.createdir(cordova.file.externalrootdirectory+folderpath, foldername, true); }); 

note $cordovafile.createdir will throw error if fails, , since you're not using result/returned value, can return without , handle error in higher level .catch().

i'm not familiar enough ionic best practice , what's not, if it's necessary wrap createfolder() function inside $ionicplatform.ready, since resolve directly if called , it's ready.

rewriting promise chain nice thing promises can rid of callback hell/pyramid of doom happens when have alot of asynchronous functions takes callbacks. you've done use promises in nested way doesn't make neither easier nor more readable human eye. might want consider refactoring functions more readable.

when return promise .then() wait until promise resolved before continuing next one. when return value .then() handler, wrapped , resolved next function in promise chain. thus, rewrite code below.

define functions

function createfirstfolder(result){      $scope.projectdetails.clientid = result.insertid;      // create folder name in renewate folder      return projectcreated.createfolder("renewate", $scope.projectdetails.clientname); }  function createnewlocation(){     // create new location     var parameters= [$scope.projectdetails.clientid, $scope.projectdetails.locationname];     return newproject.insertlocation(parameters); }   function createsecondlevelfolder(result){     $scope.projectdetails.locationid = result.insertid;     // create folder name in renewate folder     var folderpath = "renewate"+"/"+$scope.projectdetails.clientname;     return projectcreated.createfolder(folderpath, $scope.projectdetails.locationname); }  function insertnewproject(){       var parameters = [$scope.projectdetails.projectname, $scope.projectdetails.locationid];       return newproject.insertproject(parameters); }  function createnewprojectfolder(result){     $scope.projectdetails.projectid = result.insertid;     var folderpath = "renewate"+"/"+$scope.projectdetails.clientname + "/" + $scope.projectdetails.locationname;     return projectcreated.createfolder(folderpath, $scope.projectdetails.projectname); } 

and call them this

if( $scope.projectdetails.clientid == null && $scope.projectdetails.locationid == null ){     // insert row in client table     var parameters=[$scope.projectdetails.clientname];      newproject.insertclient(parameters)       .then(createfirstfolder)       .then(createnewlocation)       .then(createsecondlevelfolder)       .then(insertnewproject)       .then(createnewprojectfolder)       .then(function(){         //$state.go('renewate3'); ?       })       .catch(function(err) {         //handle error       }); } 

which should alot more easier on eye easier debug!


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -