Created
October 11, 2017 21:58
-
-
Save giovanniramos/11cf3a72147baf5f1b1bc83de35e328e to your computer and use it in GitHub Desktop.
AngularJS - How to Cancel / Abort all pending requests in angularJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** ************************************************* | |
File: app.module.js | |
*/ | |
(function () { | |
'use strict'; | |
// Defining angularjs Module | |
var app = angular | |
.module('appMaster'); | |
// Ajax provider | |
app.provider('$ajax', [function $ajaxProvider($http, $q) { | |
this.$get = function ($http, $q) { | |
var resolved = false; | |
var currentUrl = null; | |
var deferred = deferred || $q.defer(); | |
var cancel = function () { | |
deferred.resolve('http call aborted'); | |
}; | |
return { | |
get: function (url, config, callback) { | |
if (typeof config === 'function') { | |
callback = config; | |
config = {}; | |
} | |
config = config || {}; | |
if (resolved && currentUrl == url) { | |
cancel(); | |
} | |
resolved = true; | |
currentUrl = url; | |
deferred = $q.defer(); | |
config.url = url; | |
config.method = config.type || 'GET'; | |
config.data = !$.isEmptyObject(config.data) ? angular.toJson(config.data) : {}; | |
config.params = config.params || {}; | |
config.headers = { 'Content-Type': 'application/json' }; | |
config.timeout = deferred.promise; | |
var promise = $http(config); | |
promise.then( | |
function (answer) { | |
var data = answer.data; | |
var checkData = ""; | |
try { | |
checkData = angular.fromJson(data.d != undefined ? data.d : data); | |
} catch (e) { | |
checkData = data; | |
} | |
resolved = false; | |
if (typeof callback == "function") callback(checkData); | |
return $q.resolve(answer); | |
}, | |
function (reason) { | |
if (typeof callback == "function") callback(reason); | |
return $q.reject(reason); | |
} | |
); | |
return promise; | |
}, | |
post: function (url, config, callback) { | |
config = config || {}; | |
config.type = 'POST'; | |
return this.get(url, config, callback); | |
} | |
}; | |
}; | |
}]); | |
}()); | |
/** ************************************************* | |
File: main.factory.js | |
*/ | |
(function () { | |
'use strict'; | |
var app = angular | |
.module('appMaster') | |
.factory('mainFactory', mainFactory); | |
mainFactory.$inject = ['$http', '$ajax']; | |
function mainFactory($http, $ajax) { | |
var _self = [{}]; | |
// GET: /GetProfiles | |
_self.GetProfiles = function () { | |
return $ajax.get('/api/master/GetProfiles'); | |
}; | |
// GET: /GetProfileById | |
_self.GetProfileById = function (idProfile) { | |
var config = { | |
params: { idProfile: idProfile } | |
}; | |
return $ajax.get('/api/master/GetProfileById', config); | |
// OR | |
//return $ajax.get('/api/master/GetProfileById/' + idProfile); | |
}; | |
// POST: /ProfileUpdate | |
_self.ProfileUpdate = function (idProfile, name, surname) { | |
var config = { | |
data: { idProfile: idProfile, name: name, surname: surname } | |
}; | |
return $ajax.post('/api/master/ProfileUpdate', config); | |
}; | |
return _self; | |
} | |
}()); | |
/** ************************************************* | |
File: main.controller.js | |
*/ | |
(function () { | |
'use strict'; | |
var app = angular | |
.module('appMaster') | |
.controller('MainController', MainController); | |
MainController.$inject = ['$scope', 'mainFactory']; | |
function MainController($scope, mainFactory) { | |
var vm = $scope; | |
vm.Profile = {}; | |
vm.LstProfiles = []; | |
// GetProfileById | |
vm.GetProfileById = function (idProfile) { | |
vm.Profile = {}; | |
mainFactory.GetProfileById(idProfile).then( | |
function successCallback(response) { | |
var _data = response.data; | |
vm.Profile = _data; | |
}, | |
function errorCallback(response) { | |
console.log('Error: ', response.data); | |
} | |
); | |
}; | |
// GetProfiles | |
vm.GetProfiles = function () { | |
vm.LstProfiles = []; | |
mainFactory.GetProfiles().then( | |
function successCallback(response) { | |
var _data = response.data; | |
vm.LstProfiles = _data; | |
}, | |
function errorCallback(response) { | |
console.log('Error: ', response.data); | |
} | |
); | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment