Currently, I am in the process of developing an application that includes the following 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));
}
});
}
}
})
The method Caregiver.getService(id)
retrieves the Service with the specified ID through an AJAX request. If the ID is undefined, the next upcoming Service for the current day will be returned.
My goal is to ensure that the application always redirects to /service/:id
once we have determined which Service (and its ID) needs to be displayed. When a user accesses /service
, I aim to execute
$location.replace('/service/' + service.id);
seamlessly. However, if a redirection is implemented, the resolve function will execute again, resulting in a duplicate fetch of the event.
I have attempted various workarounds, such as this, to prevent the page from reloading once the user is already inside. Although the controller runs only once, the resolve function in the $routeProvider is still triggered twice.