Created
October 11, 2017 02:38
-
-
Save cuonggt/87798ae67439b17142c4259f2965b7a7 to your computer and use it in GitHub Desktop.
AngularJS: Adding One Resolve to All Routes
This file contains hidden or 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
angular | |
.module('app') | |
.config(config); | |
function config($routeProvider, $locationProvider) { | |
var universalResolves = { | |
catalogIds: function ($http, $rootScope, $localStorage, $q) { | |
if ($localStorage.catalogIds) { | |
return $q.when($localStorage.catalogIds); | |
} | |
return $http.post($rootScope.app.gatewayApiUrl, { | |
'wsCode': 'ptg_getSiteMap', | |
'wsRequest': { | |
'SiteType': $rootScope.app.siteType, | |
'Language': $rootScope.app.language, | |
'Domain': $rootScope.app.domain, | |
'Type': 2 | |
} | |
}) | |
.then(response => { | |
// return response.data.result.wsResponse; | |
$rootScope.app.headerMenus = response.data.result.wsResponse; | |
$rootScope.app.catalogIds = {}; | |
angular.forEach($rootScope.app.headerMenus, function (value, key) { | |
$rootScope.app.catalogIds[value.code] = value.catalogId; | |
}); | |
$localStorage.catalogIds = $rootScope.app.catalogIds; | |
return $rootScope.app.catalogIds; | |
}); | |
} | |
}; | |
var customRouteProvider = angular.extend({}, $routeProvider, { | |
when: function (path, route) { | |
route.resolve = (route.resolve) ? route.resolve : {}; | |
angular.extend(route.resolve, universalResolves); | |
$routeProvider.when(path, route); | |
return this; | |
} | |
}) | |
customRouteProvider | |
.when('/', { | |
templateUrl: 'pages/home.html', | |
controller: 'HomeController', | |
resolve: {} | |
}); | |
$locationProvider.html5Mode(true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment