Skip to content

Instantly share code, notes, and snippets.

@davemo
Last active November 24, 2016 10:06
Show Gist options
  • Save davemo/5815716 to your computer and use it in GitHub Desktop.
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.
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' });
});
@jeremyml
Copy link

@vwasteels
Copy link

@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