Angular chaining promises from foreach loop - angularjs

I have an array of photo files that needed to upload to Azure Cloud Storage, and i using foreach loop to call upload as below:
$scope.savetemplate = function () {
var imagePathsArray = [];
$scope.filesimage = [];
$scope.filesimage.push($scope.file1);
$scope.filesimage.push($scope.file2);
$scope.filesimage.push($scope.file3);
for (var i in $scope.filesimage) {
$scope.upload($scope.filesimage[i]);
}
$scope.data.Images = imagePathsArray ;
$http({
//after finish uploads i need to post the paths
//of all images to save into database
})
};
$scope.upload = function (file) {
Upload.upload({
url: '/uploadImage',
data: { file: file }
}).then(function (resp) {
imagePathsArray.push(resp.data);
})
};
resp.data returns azure storage path and i need to push the paths into the imagePathsArray
How can i uses Angular Promise to wait for upload all the files finished and all the paths are stored in the imagePathsArray so i can proceed with
$scope.data.Images = imagePathsArray ;
so that i can get the paths in the array and perform $http post?

You can do that with $q.all.
var promises = [];
for (var i in $scope.filesimage) {
promises.push(upload($scope.filesimage[i]));
}
$q.all(promises).then(function() {
$scope.data.Images = imagePathsArray ;
$http.post({
//after finish uploads i need to post the paths
//of all images to save into database
});
});
function upload(file) {
return Upload.upload({
url: '/uploadImage',
data: { file: file }
}).then(function (resp) {
imagePathsArray.push(resp.data);
})
};

In the success callback of the upload function, after pushing the path:
imagePathsArray.push(resp.data);
if(imagePathsArray.length == $scope.filesimage.length){
pushtoDatabase();
}
Inside pushtoDatabase call the $http({ .... });
NOTE : You might like to consider the probability of the upload getting failed. In that case you can work-around using a counter of failed files say failCounter , and then inside the if check for the condition
if ((imagePathsArray.length + failCounter) == $scope.filesimage.length){....}

Related

AngularJS get value from API only if not already set

I have service to get some data from API and serve them to application.
Simple function like this:
getEnvironmentStatus() {
var _this = this;
var req = {
method: "GET",
url: "/api/system/hosting",
headers: {},
data: {}
}
return _this.$http(req);
}
In some other place I have:
determineHostingEnv() {
var _this = this;
this.$env.getEnvironmentStatus()
.then(function(response){
_this.EnvHositng = response.data.cloud_hosted;
}, function(error) {
});
}
If I need the same information in other place (other controller), I would need to call api again.
How can I make getEnvironmentStatus() function to call API only once and store data in local variable, so it can serve that variable next time it is asked for it, instead of calling API?
Also, what if that value will get requested a few times before the first API will return value? Can I prevent calling that API a few times?
One can cache the promise:
httpPromiseCache = null;
getEnvironmentStatus() {
var _this = this;
var req = {
method: "GET",
url: "/api/system/hosting",
headers: {},
data: {}
}
if (!_this.httpPromiseCache) _this.httpPromiseCache = _this.$http(req);
return _this.httpPromiseCache;
}
The service will only execute the HTTP request once.

ng-file-upload within ngResource ng-multiple progress

I have no clue how to dispatch multiple files into one server hit using the ngResource service module.
Below shows passing one file in the callback - that works.
However, if all the files are passed in one call to o.Upload(loParams.files...), Fiddler sees the file count correctly but each hit is missing the data of the file upload.
Then there is the entire seperate issue of showing $progress.
Has anyone besides me played around with this and gotten it working?
var o = $resource('myResource',{}, (
'Upload': {url: 'fileUpload',
method: 'POST',
isArray: false,
transformRequest: formDataObject,
head
});
return {
Upload: function (loParams, dataCallback, errorCallback) {
var laResponse = [];
for (var i = 0; i < loParams.files.length; i++) {
// copy parameters than add the file before uploading
var loFileUpload = angular.copy(loParams)
loFileUpload.files = loParams.files[i];
oDBC.Upload(loFileUpload, function (response) {
laResponse.push(response);
}, function (error) {
errorCallback(error);
});
};
}
Angular.Resource
GitHub :: ng-file-upload

Promise Chains in angularjs

I am uploading attachments using rest api in SharePoint 2013,for this I need to call upload attachment method on synchronous.
Because If I call upload attachment method asynchronous I am getting 409 conflict error.
How to chain promise objects in for loop.i.e I want to call second attachment method in first attachment success and so on..
Please help me in best approach of chaining of promises in for loop.
Common method for saving attachments:
var saveFileAngularJS = function (file, url) {
var deferred = $q.defer();
getFileBuffer(file).then(function (fileArrBuffer) {
$http({
method: 'POST',
url: baseUrl + url,
headers: {
'Accept': 'application/json;odata=verbose',
'Content-Type': undefined,
'X-RequestDigest': jQuery("#__REQUESTDIGEST").val()
},
data: new Uint8Array(fileArrBuffer),
transformRequest: []
}).then(function successCallback(data) {
deferred.resolve(data);
alert('Successfully saved.', data);
}, function errorCallback(error) {
deferred.reject(error);
alert('Failed to save!!!.', error);
});
});
return deferred.promise;
};
Method calling :
for (var i = 0; i < $scope.files.length; i++) {
var file = $scope.files[i]._file;
var response = lssDealService.insertAttachment(transactionId, file);
}
var insertAttachment = function (dealId, file) {
var attachmentUrl = listEndPoint + "/GetByTitle('TransactionList')/GetItemById(" + dealId + ")/AttachmentFiles/add(FileName='" + file.name + "')";
return baseService.saveFile(file, attachmentUrl);
};
Insert attachment will call SaveFile method.
I want to run this for loop sequentially, once the loop has been completed I need to process all promises and display success message to user.
Please help me to writing the chaining promises in effective way.
Lets say you have the attachements as an array,
function uploadMyAttachements() {
return myAttachements.reduce(function(promise, attachment) {
return promise.then(function () {
return upload(attachment);
})
.then(function(result) {
console.log('RESULT FOR LAST UPLOAD', result);
});
}, Promise.resolve());
}
function upload(attachment) {
//upload the attachment to sharepoint
//and return a promise here
}
uploadMyAttachements().catch(function(err) {
//if anything in the promise chain fails
//it stops then and there and CATCHED here
});
Now whats happening here, using the Array.reduce, we create a chain of promises like shown below
upload(0).then(handleResult_0).upload(1).then(handleResult_1)....
and it execute one by one as you expected
Throwing my 2 pennies:
$scope.attachments = []; //modified via binding.
function uploadAttachments(){
//Reduce the files array into a promise array with the uploadOne method
//then return the promise when every promise has been resolved or one has rejected.
return $q.all($scope.attachments.reduce(uploadOne, []));
}
function uploadOne(file){
//Upload one, return promise. Use $http or $resource.
}
//Note - a more advanced way of doing this would be to send the files as batch (one
//$http post) as FormData. There are some good wrappers for angular.
$scope.upload = function(){
uploadAttachments().then(function(results){
//Array of results
}).catch(function(e){
//Error handler
});
}

angular service and ng-file-upload by danialfarid

I declared a variable inside a function and modify it again inside another function that is within the variable function. Why the value of variable upload is reset or reinitialize when returning it?
var upload = [];
$upload.upload({
url: '/ibsapp/app/app/api/libs/upload.php',
file: file[0],
data: fileLocation,
}).success(function (data, status, headers, config) {
// add publication data
AppService.insertService('publication', publication).then(function(response){
// If unsuccessful in inserting data in the database, delete the files that are uploaded.
if(response['status'] === "error"){
$upload.upload({
url: '/ibsapp/app/app/api/dbHelper/delete.php',
data: fileLocation + '/' + file[0].name,
});
/* upload value is now the response */
upload = response;
});
}
});
/* When I log here, the value of upload is reset to [] */
console.log(upload);
return upload;

Angular js api url in loop

$http({method: 'GET', url: '/xxx/xxx/xas'}).success(function(data) {
$scope.website = data.websites;
});
$http({method: 'GET',url: '/xx/xasxxx?websiteId='+$scope.website.websiteId}).success(function(data) {
$scope.onlinedata1 = data.coupons;
});
I try to get websiteID from top url and pass that id in to 2nd url .my json data structure
"websites":[{
"websiteName":"Flipkart",
"websiteId":"1",
},
{
"websiteName":"asas",
"websiteId":"5",
}]
Try to pass every id one by one. I am using AngularJS v1.2.17.
Move the second HTTP call within the success callback of the first one:
$http({method: 'GET', url: '/xxx/xxx/xas'}).success(function(data) {
$scope.website = data.websites;
for (var i = 0; i < data.websites.length; i++)
{
$http({method: 'GET',url: '/xx/xasxxx?websiteId='+data.websites[i].websiteId}).success(function(data) {
$scope.onlinedata1 = data.coupons;
});
}
});
This can be simplified considering that your requests are GETs:
$http.get('/xxx/xxx/xas')
.then(function(res) {
for (var i = 0; i < res.data.websites.length; i++)
{
$http.get('/xx/xasxxx?websiteId='+res.data.websites[i].websiteId)
.then(function(res) {
$scope.onlinedata1 = res.data.coupons;
});
}
});
Please note that the above will issue one request for each website returned by the API. If you have control over the API you might want to consider accepting multiple website IDs on the second URL resource (/xx/xasxxx?websiteIds=1,5,7,12,56) so as to limit the number of requests issued by the client.
Use $q - service in module ng
A service that helps you run functions asynchronously, and use their return values (or exceptions) when they are done processing.

Resources