5

We're using angular-ui-router (version 0.2.10 i believe).

There are two primary ways to arrive at a state,

  • a) a user hits / ammends the url which corresponds to a state
  • b) code somewhere e.g. controller invokes $state.go().

We would like in case a) to validate the state (e.g. is it valid according to one or more business rules), but not to do so in case b) since we're pretty sure that our application will only transition to valid states.

Case a) will often require an http round trip to perform validation which we'd like to avoid if possible.

How would this be implemented?

Thanks!

6footunder
  • 1,318
  • 18
  • 29
  • 1
    Why not use resolve to check certain criteria before loading the page? You can then pass data into the controller. See Resolve on [UI-Router](https://github.com/angular-ui/ui-router/wiki) – Josh Aug 19 '14 at 13:18
  • @Josh, I was aware of resolve and had considered it. I would need to pass custom data to the stateProvider to determine the difference between a) and b) above, and then check out the legitimacy of being able to redirect if the state validation fails at this point? – 6footunder Aug 19 '14 at 20:33
  • 1
    hmm. I'm wondering if you can pass a `$stateParam` not in the url, as an object, that you can process in the resolve. See params subsection [here](http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$stateProvider#state), and [processing params in resolve](http://stackoverflow.com/questions/19012579/ui-router-resolve-with-dynamic-parameters). That should differentiate between state.go() and typing in the url. If that doesn't work, not sure – Josh Aug 19 '14 at 21:22
  • Hmm, thanks Josh. We've taken a different approach for now in that we've stopped exposing the url's (location:false) and "abstracted" them, so users will have difficulty arriving at states via the url. Not 100% the best but we have many other issues to solve! Thanks for your input. – 6footunder Aug 22 '14 at 00:16

1 Answers1

0

You could wrap the validation in a service and call it every time the state changes. However, before moving to another state with $state.go(), you instruct the validation service to consider the particular parameters you are passing as valid. E.g.

validationService.trustAsValid(yourData);
lex82
  • 11,173
  • 2
  • 44
  • 69