Skip to content

Instantly share code, notes, and snippets.

@davemo
Last active November 24, 2016 10:06
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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' });
});
@voronianski
Copy link

nice suggest! however it looks like mistake here -

var routes = addGlobalResolveToRoutes(manifest);
// -> should be like:
var routes = addGlobalResolveToRoutes(RouteManifest);

@davemo
Copy link
Author

davemo commented Apr 21, 2014

Corrected, thanks @voronianski :)

@jimthedev
Copy link

Would this work for Angular UI's $stateService as well?

@fgarit
Copy link

fgarit commented Sep 9, 2014

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:

  • Line 27: if the config.resolve is undefined, then the _.extend doesn't seem to work
  • Line 32: since the addGlobalResolveToRoutes doesn't have a return statement, we shouldn't assign its value to the routes variable.
  • Line 34, path and config should be swapped.

So in the end, the following is working for me:

myApp.config([ '$routeProvider', 'RouteManifest', function($routeProvider, RouteManifest) {
  var addGlobalResolveToRoutes = function(manifest) {
    _(manifest).each(function(config, path) {
      var extendedResolveConfig = _(config.resolve || {}).extend({ nameOfGlobalResolve: function() { ... }});
      manifest[path].resolve = extendedResolveConfig;
    });
  };

  addGlobalResolveToRoutes(RouteManifest);
  _(RouteManifest).each(function(config, path) {
    $routeProvider.when(path, config);
  });

  $routeProvider.otherwise({
    redirectTo : '/login'
  });
} ]);

Still, thanks for the snippet :)

@tictcotq
Copy link

Another (and simpler) way to to this: http://stackoverflow.com/a/19938307

@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