Last active
September 26, 2025 13:37
-
-
Save davemo/5815716 to your computer and use it in GitHub Desktop.
Maybe you want to add a global "resolve" property to all routes in an angular app, this is one way you could achieve that. I would probably still factor out the logic inside my app.config block into some sort of Service abstraction, but this should serve as enough of a general idea.
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
| app.constant("RouteManifest", { | |
| "/login" : { | |
| templateUrl: 'templates/login.html', | |
| controller: 'LoginController' | |
| }, | |
| "/home" : { | |
| templateUrl: 'templates/home.html', | |
| controller: 'HomeController' | |
| }, | |
| "/books" : { | |
| templateUrl: 'templates/books.html', | |
| controller: 'BooksController', | |
| resolve: { | |
| books : function(BookService) { | |
| return BookService.get(); | |
| } | |
| } | |
| } | |
| }); | |
| app.config(function($routeProvider, RouteManifest) { | |
| var addGlobalResolveToRoutes = function(manifest) { | |
| _(manifest).each(function(config, path) { | |
| var extendedResolveConfig = _(config.resolve).extend({ nameOfGlobalResolve: function() { ... }}); | |
| manifest[path].resolve = extendedResolveConfig; | |
| }); | |
| }; | |
| var routes = addGlobalResolveToRoutes(RouteManifest); | |
| _(routes).each(function(path, config) { | |
| $routeProvider.when(path, config); | |
| }); | |
| $routeProvider.otherwise({ redirectTo: '/login' }); | |
| }); |
@davemo shouldn't the addGlobalResolveToRoutesfunction return the manifest modified ? otherwise I don't see how var routes = addGlobalResolveToRoutes(RouteManifest); could return something ... ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is another discussion of a solutoin: https://spin.atomicobject.com/2014/10/04/javascript-angularjs-resolve-routes/