Last active
November 24, 2016 10:06
-
-
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' }); | |
}); |
Another (and simpler) way to to this: http://stackoverflow.com/a/19938307
Here is another discussion of a solutoin: https://spin.atomicobject.com/2014/10/04/javascript-angularjs-resolve-routes/
@davemo shouldn't the addGlobalResolveToRoutes
function 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
I could be wrong (I'm a newbie in javascript, undescore, angular and all that) but it seems to me like there are some issues in the code:
So in the end, the following is working for me:
Still, thanks for the snippet :)