diff --git a/app/app.js b/app/app.js index 317ff2d..a7bd786 100644 --- a/app/app.js +++ b/app/app.js @@ -33,9 +33,9 @@ angular.module('bansho', [ }]) // Reinitialise objects on url change - .run(['$rootScope', 'clearAjaxPromises', 'reinitTables', function ($rootScope, clearAjaxPromises, reinitTables) { + .run(['$rootScope', 'promisesManager', 'reinitTables', function ($rootScope, promisesManager, reinitTables) { $rootScope.$on('$locationChangeStart', function () { reinitTables(); - clearAjaxPromises(); + promisesManager.clearAllPromises(); }); }]); diff --git a/app/components/table/table.js b/app/components/table/table.js index 42f8bb5..d40be8a 100644 --- a/app/components/table/table.js +++ b/app/components/table/table.js @@ -17,8 +17,8 @@ angular.module('bansho.table', ['bansho.live', .value('tablesConfig', []) .controller('TableCtrl', ['$scope', '$interval', 'getTableData', 'tablesConfig', - 'actionbarFilters', 'addAjaxPromise', 'tableGlobalConfig', - function ($scope, $interval, getTableData, tablesConfig, actionbarFilters, addAjaxPromise, tableGlobalConfig) { + 'actionbarFilters', 'promisesManager', 'tableGlobalConfig', + function ($scope, $interval, getTableData, tablesConfig, actionbarFilters, promisesManager, tableGlobalConfig) { var requestFields = [], conf = tablesConfig[tableGlobalConfig.nextTableIndex], getData, @@ -51,7 +51,7 @@ angular.module('bansho.table', ['bansho.live', getData(requestFields, conf.filters, conf.apiName, conf.additionnalQueryFields); if (tableGlobalConfig.refreshInterval !== 0) { - addAjaxPromise( + promisesManager.addAjaxPromise( $interval(function () { getData(requestFields, conf.filters, conf.apiName, conf.additionnalQueryFields); }, tableGlobalConfig.refreshInterval) diff --git a/app/components/tactical/tactical.js b/app/components/tactical/tactical.js index f557737..44d1d63 100644 --- a/app/components/tactical/tactical.js +++ b/app/components/tactical/tactical.js @@ -17,9 +17,9 @@ angular.module('bansho.tactical', ['bansho.live', }) .controller('TacticalCtrl', ['$scope', '$interval', 'tacticalConfig', 'getHostProblems', 'getServiceProblems', - 'getTotalHosts', 'getTotalServices', 'addAjaxPromise', + 'getTotalHosts', 'getTotalServices', 'promisesManager', function ($scope, $interval, tacticalConfig, getHostProblems, getServiceProblems, getTotalHosts, - getTotalServices, addAjaxPromise) { + getTotalServices, promisesManager) { var getData; @@ -53,7 +53,7 @@ angular.module('bansho.tactical', ['bansho.live', }; if (tacticalConfig.refreshInterval !== 0) { - addAjaxPromise( + promisesManager.addAjaxPromise( $interval(getData, tacticalConfig.refreshInterval) ); } diff --git a/app/components/topbar/topbar.js b/app/components/topbar/topbar.js index 9db55d2..c46335a 100644 --- a/app/components/topbar/topbar.js +++ b/app/components/topbar/topbar.js @@ -2,8 +2,8 @@ angular.module('bansho.topbar', ['bansho.live']) - .controller('TopBarCtrl', ['$scope', '$interval', 'getServiceProblems', 'getHostProblems', 'addAjaxPromise', - function ($scope, $interval, getServiceProblems, getHostProblems, addAjaxPromise) { + .controller('TopBarCtrl', ['$scope', '$interval', 'getServiceProblems', 'getHostProblems', 'promisesManager', + function ($scope, $interval, getServiceProblems, getHostProblems, promisesManager) { var getData, hostProblems, serviceProblems; @@ -19,7 +19,7 @@ angular.module('bansho.topbar', ['bansho.live']) }; // TODO: Change hardcoded interval when the topbar dashboard will be implemented - addAjaxPromise($interval(getData, 10000)); + promisesManager.addAjaxPromise($interval(getData, 10000)); getData(); }]) diff --git a/app/components/utils/promise_manager.js b/app/components/utils/promise_manager.js index 04c4deb..a226229 100644 --- a/app/components/utils/promise_manager.js +++ b/app/components/utils/promise_manager.js @@ -2,25 +2,27 @@ angular.module('bansho.utils.promiseManager', []) - .value('ajaxPromises', []) + .service('promisesManager', ['$interval', function ($interval) { + var ajaxPromises = []; - .service('addAjaxPromise', ['ajaxPromises', function (ajaxPromises) { - return function (promise) { - ajaxPromises.push(promise); - }; - }]) - - .service('clearAjaxPromises', ['$interval', 'ajaxPromises', function ($interval, ajaxPromises) { - return function () { + function clearAjaxPromises () { angular.forEach(ajaxPromises, function (promise) { $interval.cancel(promise); }); - }; - }]) + } - .service('clearAllPromises', ['ajaxPromises', 'clearAjaxPromises', - function (ajaxPromises, clearAjaxPromises) { - return function () { - clearAjaxPromises(); - }; - }]); + /** + * Add a new promise to check + * @param promise + */ + this.addAjaxPromise = function (promise) { + ajaxPromises.push(promise); + }; + + /** + * Clear all types of promises + */ + this.clearAllPromises = function () { + clearAjaxPromises(); + }; + }]); diff --git a/app/templates/dashboard/dashboard.js b/app/templates/dashboard/dashboard.js index 84a200e..39989f5 100644 --- a/app/templates/dashboard/dashboard.js +++ b/app/templates/dashboard/dashboard.js @@ -11,9 +11,9 @@ angular.module('bansho.view.dashboard', ['ngRoute', .controller('DashboardCtrl', ['$scope', '$routeParams', '$interval', 'dashboardConfig', 'getObjects', 'TableConfigObj', 'TacticalConfigObj', 'getHostOpenProblems', 'getServiceOpenProblems', 'getHostProblems', - 'getServiceProblems', 'addAjaxPromise', + 'getServiceProblems', 'promisesManager', function ($scope, $routeParams, $interval, dashboardConfig, getObjects, TableConfigObj, TacticalConfigObj, getHostOpenProblems, - getServiceOpenProblems, getHostProblems, getServiceProblems, addAjaxPromise) { + getServiceOpenProblems, getHostProblems, getServiceProblems, promisesManager) { var components = [], component, config, @@ -60,7 +60,7 @@ angular.module('bansho.view.dashboard', ['ngRoute', }; if ($scope.dashboardRefreshInterval !== 0) { - addAjaxPromise( + promisesManager.addAjaxPromise( $interval(getData, $scope.dashboardRefreshInterval * 1000) ); }