I am trying to save the tags for selected items list using api. In api I store the tags only once for the first selected item and for the rest I do not store the items instead I create the entry as shared. Problem is: the first selected item hits the api and before storing the data other selected items also gets hit and I am not able to provide the shared reference as the data is not been stored yet.
Things I have tried -
This is my controller:
let selectedQstCount = $scope.selectedQuestion.length;
for (i = 0; i < selectedQstCount; i++) {
var params = {
"jsonString": callbackData,
"questionNo": $scope.Questionlst.find(a => a.QuestionId == $scope.selectedQuestion[i].id).QuestionNo,
"projectName": $scope.ProjectName,
"projectId": $scope.ProjectId,
"questionId": $scope.selectedQuestion[i].id,
"newCodeFrame": ($scope.attachCodeFrame == "No" ? "FALSE" : "TRUE"),
"sharedQstId": $scope.sharedQstId,
"groupName": $scope.groupName
}
var defer = $q.defer();
CodeFrameJsonfactory.CodeFrameUpload(params, function (data) {
//success
$q.resolve(data);
}, function (callbackData) {
//error
inform.clear();
inform.add(callbackData.Message, { ttl: -1, type: 'danger' });
});
return defer.promise;
}
This is my factory:
CodeFrameUpload: function (params, successCallback, errorCallback) {
var deferred = $q.defer();
$http({
method: 'POST',
url: APIUrls.getUrl("uploadCodeFrame"),
data: params,
headers: {
'Content-Type': 'application/json'
},
cache: false
}).success(function (data) {
deferred.resolve(data);
successCallback(data);
}).error(function (data) {
deferred.reject(data);
errorCallback(data);
});
return deferred.promise;
},
I have also tried using angular.forEach with deffered.
I want achieve is, until CodeFrameUpload function is completed I do not want to hit it again.