Skip to content Skip to sidebar Skip to footer

Custom Route Provider When Is Not A Function

This is an updated version of a question from this morning. I have two separate modules, 'admin' and 'authorization'. In the provider block of authorization, I extend $routeProvide

Solution 1:

Just regarding the error, the $appRoute provider is not exposing any methods on it self (other then $get). So its an empty api config provider object.

When using the base provider recipe, the $get method is used to provide a function to generate the service, and the this from the provider is used to bind extra config functions to put data the $get function can later use.

From Angular provider docs:

myApp.provider('unicornLauncher', functionUnicornLauncherProvider() {
  var useTinfoilShielding = false;

  // provider config api method.this.useTinfoilShielding = function(value) {
    useTinfoilShielding = !!value;
  };

  this.$get = ["apiToken", functionunicornLauncherFactory(apiToken) {

    // let's assume that the UnicornLauncher constructor was also changed to// accept and use the useTinfoilShielding argumentreturnnewUnicornLauncher(apiToken, useTinfoilShielding);
  }];
});

If you want to add additional methods on the provider, you can use this.when = ... or Angular.extends(this, ....) for instance.

Solution 2:

I ended up going with a simpler solution than extending the route provider.

In my "top-level" module, I modified all routes with the following code:

angular.module('app'[])
    .run(function($route) {
        var keys = Object.keys($route.routes);
        keys.forEach(function(key) {
            var modifiedRoute = $route.routes[key];

            // if there's already a resolve function defined, don't overwrite it, just add a new one
            modifiedRoute.resolve = key.resolve?key.resolve:{};
            modifiedRoute.resolve.authorize = function($authorization) {
                return$authorization.authorize();
            }

            $route[key] = modifiedRoute;
        });
     });

This works because the routes were already defined in the other modules .config() blocks, which executed before the run() block. It's slightly ugly, but it works.

Post a Comment for "Custom Route Provider When Is Not A Function"