-
Notifications
You must be signed in to change notification settings - Fork 3k
Expose $urlRouterUpdateStart
event to allow deferrable update()
#591
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
Conversation
I'm curious why this can't be done within $stateChangeStart. Is it because by then the url has already changed? |
@timkindberg I looked at this event too and yes it's generally because the state transition promises etc are already set in motion and it was very difficult to inject a new promise to defer / replay the transition etc. Users may also want deferred logic that effects the I also looked at Definitely open to other implementations but I do think ui router would benefit greatly by having some sort of deferrable method to load user information / other configuration of an application before a state transition happens with the ability to choose if the deferrable method is run only once, or on every state change call etc. I actually wonder if it would be possible / plausible to write into angular core an option of passing a promise to |
There are different ways to handle this use case, of which this is only one. For example, in apps I write, states that can't be transitioned to almost always require API calls that don't resolve until the user has authenticated (hence, the states that trigger them never resolve). A solution that would be cleaner and require less overhead would be to have |
@nateabele Yes I used to take the "grandfather" approach by having a state that resolved it's dependencies first. However I've recently been working with route protection via I prefer your proposed solution, updating PR now. |
@nateabele Updated PR. Sorry my other machine obviously has git default setup incorrectly. |
@2ix No worries. Sorry if I wasn't clear in my previous explanation, but the point was specifically to avoid the overhead of an extra event dispatch. In other words, The idea was to use the existing event, as follows: function update($event) {
if ($event && $event.defaultPrevented) return;
/* ... */
} The current interception of Make sense? |
@nateabele No worries, committed the change for you. |
@2ix Looks perfect. Squash your commits & we'll roll it. |
… as .sync() alias
@nateabele Great! How's this? |
Expose `$urlRouterUpdateStart` event to allow deferrable `update()`
@2ix 👍 |
@nateabele When do you expect this to be built into release for bower users? |
In many complex applications the first question that is asked is how can I asynchronously request user information before any
$stateChangeStart
events trigger. Whether it be to incorporate route protection, configuration, redirection etc.This proposed pull request seeks to answer that question by exposing a new event
$urlRouterUpdateStart
which can cancel$urlRouter#update()
withpreventDefault()
.update()
is then referenced in the$urlRouterUpdateStart
event listener and can be called asynchronously/synchronously.The following example deferrers
update()
for 2000ms using$timeout
asynchronously. The listener's deregistration function is assigned to$off
and called when the listener is fired so that the listener will only deferupdate()
once.