I know I can resolve certain dependencies for a controller based on the route. However, I have a service that I always want to be resolved: The current user.
The service basically does a get request to the server for the current user (url is something like /api/user/me
) and returns an object with the userid, name etc. This is currently cached and returned with promises, so everywhere I want to use it I have to do something like:
CurrentUser.getCurrentUser().then(function(user) {
// use user.id here
});
creating an extra, unnecessary layer of nesting and callbacks for data that is already loaded. Is there a nice way I can preload the user globally so I can use it everywhere without having to wait for promises?
Something along the lines of the route resolving I linked to above, except that it should work for all routes and possibly services as well.
I picture a result like this:
// somewhere
AlwaysResolveThisBeforeDoingSomething(UserService.resolve);
// In my controller
.controller('CurrentUserCtrl', function ($scope, $http, CurrentUser) {
// CurrentUser is an initialized user containing id, name etc.