0

I am working on an application that has (among others) this endpoint:

.when('/service/:id?', {
    templateUrl: 'views/service.html',
    controller: 'ServiceCtrl',
    resolve: {
      service: function($q, Caregiver, $route) {
        return $q(function(resolve, reject) {
          if (!Caregiver.isLoggedIn()) {
            reject();
          } else {
            resolve(Caregiver.getService($route.current.params.id));
          }
        });
      }
    }
  })

Caregiver.getService(id) will get me the Service with that ID, requesting it through AJAX. But since it's an optional parameter, if it's undefined, the Service returned will be the next one taking place today.

What I want is that no matter what case it is, the application redirects to /service/:id, once we know which Service (and its ID) we will be showing. If the user goes to /service, I want to make a $location.replace('/service/' + service.id);, so that it's completely transparent. But, if I do a redirection then the resolve will be executed again, and fetch the same event twice.

I've tried hacks like this to disallow the reload of the page once we're inside, and I can see that the controller runs only once, but the resolve in the $routeProvider is still being fired twice.

Community
  • 1
  • 1
manutenfruits
  • 1,447
  • 3
  • 17
  • 25

1 Answers1

0

You could try to register a route fo '/service' that simply redirects

redirectTo – {(string|function())=} – value to update $location path with and trigger route redirection.

Documentation is here

If redirectTo is a function, it will be called with the following parameters:

{Object.} - route parameters extracted from the current $location.path() by applying the current route templateUrl. {string} - current $location.path() {Object} - current $location.search() The custom redirectTo function is expected to return a string which will be used to update $location.path() and $location.search().

If this doesn't work you could try to improve the service that fetches events, caching the response that you get with no arguments and avoiding the fetch on the second request

JackNova
  • 3,911
  • 5
  • 31
  • 49
  • Yes, I tried that too (sorry I forgot), but the function in redirectTo isn't injectable, and I need to check with the Caregiver service in order to see where to redirect. – manutenfruits Mar 25 '15 at 10:14