ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

Apply a custom animation to ui-view during resolve

Open jonrimmer opened this issue 13 years ago • 57 comments

It would be very useful if ui-view ran a custom "resolve" animation between the beginning and end of a state change that affects it. This could be used to display a spinner or other loading animation, which is vital when the resolve includes fetching things from the server.

jonrimmer avatar Sep 23 '13 12:09 jonrimmer

The API provides you with two event hooks: $stateChangeStart and $stateChangeSuccess. Use them wisely.

nateabele avatar Sep 23 '13 12:09 nateabele

But how can I tell if a state change applies to a particular ui-view instance without relying on/reinventing private implementation details of the view directive? Since the directive already has access to these, wouldn't it be easier if it exposed this capability?

jonrimmer avatar Sep 23 '13 12:09 jonrimmer

Okay, could you post some example code of how you would envision using this? Because implementing it as-described would mean all kinds of abstraction boundary violations.

nateabele avatar Sep 23 '13 12:09 nateabele

I envisage that, when a relevant state change starts, the ui-view would apply a new ui-resolve CSS class to the element (Perhaps using the new $animate service? It has addClass() and removeClass() methods). The idea is that it should work very similarly to how enter, leave and move already work.

If I have following element:

<div class="main" ui-view="main">

Then during the resolve state of a relevant state change, the following CSS class would be applied:

<div class="main ui-resolve" ui-view="main">

Which I could then use to apply a transparent overlay and loading spinner:

.main.ui-resolve::after {
  position: absolute;
  content: '';
  width: 100%;
  height: 100%;
  background: rgba(255,255,255,0.5) url('spinner.gif') no-repeat center center;
}

When the resolve was finished, and the state change ended, the ui-resolve class would be removed, and the ui-view would animate the transition between the old view and the new as normal.

jonrimmer avatar Sep 23 '13 12:09 jonrimmer

@JonRimmer you could do it, but "globally" - adding class to the body element on $stateChangeStart and $stateChangeSuccess events

no way to get the currently resolving state's element, afaik

stryju avatar Apr 08 '14 13:04 stryju

@stryju - That approach results in a horrible UI experience. The user clicks on a button to refresh a list or something, and the entire screen gets blanketed in a generic spinner because the library has no way of showing a spinner just for the view(s) that are actually affected by the state change. No thanks.

In any case, after I filed this issue, I realised that the "resolve" feature is an anti-pattern. It waits for all the promises to resolve then animates the state change. This is completely wrong - you want your transition animations between states to run parallel to your data loads, so that the latter can be covered up by the former.

For example, imagine your have a list of items, and clicking on one of them hides the list and shows the item's details in a different view. If we have an async load for the item details that takes, on average, 400ms, then we can cover up the load almost entirely in most cases by having a 300ms "leave" animation on the list view, and a 300ms "enter" animation on the item details view. That way we provide a slicker feel to the UI and can avoid showing a spinner at all in most cases.

However, this requires that we initiate the async load and the state change animation at the same moment. If we use "resolve", then the entire async animation happens before the animation starts. The user clicks, sees a spinner, then sees the transition animation. The whole state change will take ~1000ms, which is too slow.

"Resolve" could be a useful way to cache dependencies between different views if it had the option not to wait on promises, but the current behaviour, of always resolving them before the state change starts makes it almost useless, IMO. It should be avoided for any dependencies that involve async loads.

jonrimmer avatar Apr 08 '14 15:04 jonrimmer

@JonRimmer i totally agree here.

there's no silver bullet here - the "resolve" pattern is powerful, but only if you know how to use it - otherwise you end up with what seems to be an "unresponsive" application :-)

stryju avatar Apr 08 '14 15:04 stryju

@stryju I still have a hard time to understand the point of resolve promises if they make the UI seem unresponsive. I would love to use them, but if there's no way to show some loading indicator (for the affected ui-view) it makes no sense.

Are the any plans to add this feature? Is there any workaround (besides moving the resolve logic to the controller)? I checked SO but only found this question http://stackoverflow.com/q/18961332/45948

roberkules avatar May 08 '14 17:05 roberkules

@roberkules well, you COULD go with something like http://chieffancypants.github.io/angular-loading-bar/ - but it's global

you potentially could use a pub/sub pattern and broadcast which view is about to get loaded, check if it has resolve set and then use something there

stryju avatar May 08 '14 18:05 stryju

I agree with @JonRimmer and @roberkules. Showing a loader animation only on the affected view while the resolve is happening is an important behavior that many developers would need to support (especially if they work with the type of designers that I've been working with). The solution that @JonRimmer proposes (i.e. adding a ui-resolve class to the affected view) would be an ideal solution.

johnnyoshika avatar May 12 '14 20:05 johnnyoshika

In any case, after I filed this issue, I realised that the "resolve" feature is an anti-pattern. It waits for all the promises to resolve then animates the state change.

It's not an anti-pattern in itself, although the way people use it certainly can be. Perhaps some guidelines can be added to the documentation.

This is completely wrong - you want your transition animations between states to run parallel to your data loads, so that the latter can be covered up by the former.

This is kind of a hard problem, since state transitions are intended to be atomic, and a failing resolve should fail the transition, in which case the state machine's job is to ensure that the UI is left in a consistent state. I'm open to opinions on how we can achieve both objectives.

Regarding the specific problem of determining what views are affected at the outset of a state transition, I'm retooling ui-router's internals in a way that might make instrumenting this easier, but the fundamental design dictates one-way communication: $state calls $view, which pushes updates to uiView elements (which may or may not exist on the page at the time $view attempts to message them).

What we might be able to do is have $state make an extra call to $view (i.e. after $stateChangeStart fires) that allows $view to ping any affected uiViews on the page that would be subject to an update or removal on completion of a successful transition, and those uiViews can fire off events that either say 'I'm about to get updated, maybe!' or 'I might be going away in a second'. Those events can then be intercepted by the controllers responsible for said uiViews, and push UI updates accordingly.

Would that solve it?

nateabele avatar May 13 '14 00:05 nateabele

@nateabele, your last suggestion (uiViews receiving and emitting notifications that state is about to change) would be a very welcome addition. It will allow us to offer feedback to the user that something is about to change on that view. I still think @JonRimmer's suggestion of adding a class (something like ui-resolve) to the impacted view is more desirable, because it will allow us to show a spinner at the right place at the right time without any code. Is it possible to do both? Even with just the solution you propose would be very valuable, as there currently isn't a way to do any of this at all.

johnnyoshika avatar May 13 '14 01:05 johnnyoshika

I'm going to second both @nateabele's and @johnnyoshika's suggestions. Both great ideas.

Alternately, this may be a dirty word around these parts but, Ember's router forces you to load remote content by using the resolve pattern. The way it accomplishes this is that each Router (or state for ui-view) can have a loading function. When the router is resolving the data it will then execute this loading function, if it exists. If the loading function does not exist on the current route, it will then look at the parent route (if there is one) and execute it's loading function.

Maybe ui.router can take a similar approach? A loading function can be executed, then when the state is ready to be shown (controller instantiated etc.) the content within the ui-view is blown away and replaced with the correct content?

Additionally, i don't know how this will affect ng-animate integration. i'll leave that to someone with more knowledge than i.

Gwash3189 avatar May 13 '14 03:05 Gwash3189

@Gwash3189, that's a really interesting suggestion and would work quite well.

johnnyoshika avatar May 13 '14 03:05 johnnyoshika

I still think @JonRimmer's suggestion of adding a class (something like ui-resolve) to the impacted view is more desirable, because it will allow us to show a spinner at the right place at the right time without any code. Is it possible to do both?

Yeah, I don't see why not.

When the router is resolving the data it will then execute this loading function, if it exists.

Can you post some examples of how this works?

nateabele avatar May 13 '14 11:05 nateabele

The loading function that @Gwash3189 mentioned is described here:

http://emberjs.com/guides/routing/loading-and-error-substates/

johnnyoshika avatar May 13 '14 15:05 johnnyoshika

@johnnyoshika Thanks for that.

@nateabele I'll have a go at it during lunch today.

Gwash3189 avatar May 14 '14 00:05 Gwash3189

@nateabele here is a link to a plnkr with my attempt. I tried implementing this functionality outside of the ui router source code. Reason being is that i don't know it well enough to make these changes during my lunch break. I hope you can get a general idea from my example though.

Just to touch on a couple of things with the example.

  1. Each state has a resolve of 1 second. There are three states, one with no child and another state with a child.
  2. ~~There is a state constructor function i use. I no longer need this but i was to lazy to change all the code!~~
  3. The states 'another' and 'another.more' have loading functions. Something i didn't know about ui router (but i'm sure you do) is that the data property of a state is copied from a parent to a child. This means that if a parent has a loading function, each child will also have the same loading function. The function is only overwritten when the loading function is provided for a child router. This makes using the loading function from a parent super easy.
  4. I have to assign an id to my child view as that is the only way i can reference it from outside ui router.

I hope i touched on everything. If i missed anything, let me know.

Edit Cleaned up the code in the example.

Gwash3189 avatar May 14 '14 04:05 Gwash3189

@nateabele

I had another go at this problem. Again i didn't edit any ui router source. This time i had an idea i tried separating the loading function into two parts, enter and leave.

Loading.enter would run when $stateChangeStart is fired. This function displays a loading spinner. Implementing this was easy and is identical to the other plnkr i posted.

The idea behind Loading.leave is that it would execute when the resolve is complete and the content (controller etc.) is ready to be instantiated. This gave me trouble and is still unimplemented as $stateChangeSuccess is broadcasted to late. Also, any other event, such as $viewContentLoaded does not send along the state it is transferring to, which makes finding any loading function impossible.

I think the a valid solution to this problem is doing the following.

  • Allow users to add loading functions as my per my last comment, although, instead of having a single function, have the ability to specify a "enter" and "leave" function.
    • The loading.enter function would run when $stateChangeStart is fired. Or it can run as soon as ui router starts trying to resolve the new content. During this stage the element with ui-view directive should be given a class of ui-resolve-enter
    • the loading.leave function would run when ui router has finish resolving the content and is ready to place the new content into the dom. The order execution would be loading.leave, then it would place the new content into the dom. During this stage the element with the ui-view directive should be given a class of ui-resolve-leave
  • Once the loading.leave stage is complete and the new content is being placed into the dom, any other animations should be used. For example, any old functionality with enter and leave when content is being placed into the dom should still work. This will allow users to display different animations for when content is loading (resolving) and when content is being put into the dom (inserting the now resolved content)

Please let me know your thoughts. I think this is a pretty solid solution as it gives the user hooks they can use (loading.enter & leave) and perform any thing during these phases and also provides ui-resolve-enter and ui-resolve-leave classes for css animations.

Gwash3189 avatar May 15 '14 00:05 Gwash3189

@Gwash3189 what purpose does loading.leave serve? I mean after the loading is done, you just want the control flow to be what it is currently, which is to init the controller and display the template. Even in your examples, loading.leave is not really used.

pdeva avatar Jun 04 '14 22:06 pdeva