Skip to content

question: possible to use promise in stateChangeStart? #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tony-kerz opened this issue Jun 21, 2014 · 2 comments
Closed

question: possible to use promise in stateChangeStart? #1153

tony-kerz opened this issue Jun 21, 2014 · 2 comments

Comments

@tony-kerz
Copy link

i've got this setup where i'm trying to do some authorization checking in stateChangeStart, and it looks something like this (in coffee-script):

  $rootScope.$on '$stateChangeStart', (event, toState, toParams, fromState, fromParams) ->

    authentication.initCurrentUser().then (resolution) ->
      if authorization.requiresAuthentication(toState)
        $state.go 'login'
      else
        $state.go toState.name

    event.preventDefault()

so it always calls event.preventDefault() because we need to wait for the resolution of the promise returned by initCurrentUser() to figure out if we are cleared for transition, but it seems like i can't transition in the then() block and it even looks like it gets into an infinite loop.

as always, possible it's operator error, but i was just wondering off the bat if anyone with a deeper understanding of the internals can spot something obviously flawed about what i'm attempting to do.

regards,
tony.

@nateabele
Copy link
Contributor

Please follow #1257 for this. Thanks!

@Willmo36
Copy link

Willmo36 commented Oct 8, 2014

Could you show an example of how the $transition$ object would solve this issue? As far as I can tell it seems to give me a great way to read what's going on but no way to cancel a state change based on a promise.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants