Developed a custom angularjs application with ng-view for loading four different modules using route provider. Each module makes HTTP requests as shown below:
var requestManager = {
"locations": {},
"employees": {},
"items": {},
"templates": {}
};
sendRequestToServer = function (reqObj) {
var reqObj = reqObj,
httpObj = {},
defer = $q.defer();
httpObj = {
method: reqObj.method,
url: baseUrl + reqObj.url,
timeout: 90000
};
if (reqObj.method == 'GET') {
httpObj['params'] = reqObj.data;
if (reqObj.uniqueId) {
httpObj['headers'] = {
'uniqueId': uniqueId
}
}
} else if (reqObj.method == 'POST' || reqObj.method == 'PUT') {
httpObj['headers'] = {
'Content-Type': 'application/x-www-form-urlencoded'
};
httpObj['data'] = reqObj.data;
}
requestManager[reqObj.moduleName][reqObj.requestName] = $http(httpObj).success(function (data, status, headers, config) {
var dataObj = {
data: data,
status: status,
headers: headers,
config: config
};
defer.resolve(dataObj);
})
.error(function (data, status, headers, config) {
var dataObj = {
data: data,
status: status,
headers: headers,
config: config
};
defer.reject(dataObj);
});
return defer.promise;
}
Looking to improve the functionality where on changing routes from one module to another aborts any ongoing server communication requests. Attempting to handle this by listening for destroy event:
$rootScope.$on('destroyModule', function(event, moduleName) {
var requests = requestManager[moduleName]
, defer = $q.defer()
, index
, req;
for(req in requests){
//Want to abort the request
defer.reject(requests[req]);
}
})
Encountering issues with the above approach. Seeking guidance and advice on how to achieve the desired behavior. Thank you!