-1

Currently in some of my controllers I have a logic that e.g. redirects user to a different route if some conditions are met.

I'm trying to move some of this logic from controllers to route.resolve to prevent showing the "invalid" view and then redirecting user somewhere else.

I already handle authentication in route.resolve, but that's just a single line - authService.requireUser(). In case of more complex logic e.g. I need to have the current user object, fetch some resources based on route params, do some calculations on the result, redirect user to one place if some condition is met, redirect to another otherwise etc. Basically it's a lot of code and putting it all into route definitions seems not very clean.

Does anyone have a solution how to handle it? Maybe use separate "route resolver" objects? Or maybe it's better to leave it in a controller and use a flag (isLoaded) to show a spinner while page is loading and display the proper view only once everything has finished loading?

szimek
  • 6,404
  • 5
  • 32
  • 36

1 Answers1

0

You could inject a service that will have that logic. And this service could return a promise

resolve:{
      projects:['Projects', function (Projects) {
        //TODO: need to know the current user here
        return Projects.all();
      }],
      tasks:['Tasks', function (Tasks) {
        //TODO: need to know the current user here
        return Tasks.all();
      }]
    }

You could take a look at this repo with a demo app to give you some ideas.

dimirc
  • 6,347
  • 3
  • 23
  • 34
  • Thanks, that's what I've got more or less right now. The issue is that I need to do something like - authenticate user (one service), find all tasks (second service), check some conditions based on user **and** tasks objects - if condition 1 is met redirect to one URL, if condition 2 is met redirect to another, otherwise just fall through and return tasks. So this new service that would handle all this logic would be just for this particular route. I'm just wondering how to handle it - if I should create a route resolver service that keeps such logic for all routes etc. – szimek Nov 23 '13 at 17:44
  • 1
    I think creating a route resolver service will be easier to test also – dimirc Nov 23 '13 at 18:43