I have an application with Angularjs and Coldfusion. I have a form with some http queries for populating drop-down list. When the session server is out, I retrieve the status from the server, I display a dialog box (thanks to ngDialog) and I reload the page.(status 0 sent by the server because the server tries to reload an external authentication system before going on the main page of the application - not in the scope of this topic).
Here my code:
app.controller('ctrlAddContacts', function ($scope, $route, ContactService, ngDialog, $timeout){
ContactService.getCountry().success(function(countries){
$scope.countries = countries;
});
ContactService.loadCategory('undefined',0).success(function(categories, status, header, config){
$scope.categories = categories;
console.log("status:" + status);
})
.error(function (data, status, header, config) {
console.log("sessionExpired: " + sessionExpired);
console.log("ERROR ");
console.log("data: " + data);
console.log("status:" + status);
console.log("header: " + header);
console.log("config: " + config);
if (status==0) {
alert("Your session expired - The page needs to be reloaded.\nPlease note that data enter in the form will be lost");
// NGDIALOG BOX
var dialog = ngDialog.open({
template: '<p>Your session expired - The page needs to be reloaded.<br />Please note that data enter in the form will be lost</p>',
plain: true,
closeByDocument: false,
closeByEscape: false
});
setTimeout(function () {
dialog.close();
window.location = "http://myapp/index.cfm?#/add-contacts";
}, 4000);
}
}).finally(function() {
console.log("finally finished repos");
});
});
It's working but I have to do that for each ajax HTTP request.
Here I have to do the same for:
ContactService.getCountry().success(function(countries){
$scope.countries = countries;
});
Thus I wouldd like to simplify the code in order to avoid to repeat the line, could you tell me please if I can do that and how (directive or factory for instance)?
Thanks