State resolving and cached views in Beta-14

I’m developing an app using ui-router resolve and Beta-14.
When i navigate through my app states, each state resolve the needed resource.

In Navigating the Changes in Beta-14 i read that “controllers persist throughout the app’s life” and event $ionicView.loaded is fired only when a view being added to the DOM.
(Cached views are awesome :smiley: )

But…I create this pen to show the (incorrect ??) behavior.

When navigate between Home and Facts, views are cached but the event $ionicView.loaded and resolve are always fired.

Is there a method to avoid firing of resolve after first time???

If i have a list of products and switch between list-detail-list-detail-list-detail i don’t want to resolve “list” state each time.

Thanks a lot and sorry for my english!

2 Likes

+1. did you find a solution?

@adw unfortunately no…

AFAICS, this is still the case with rc-2. I’d really like cached views to behave sanely with respect to resolve

Anybody filed a bug report for this?

@tkem No… I never thought it…

Any solution yet ?, have the same problem

Have the same problem. Here is my workaround

       .state('app.customers', {
            url: "/customers",
            views: {
                'menuContent': {
                    templateUrl: "templates/index.html",
                    controller: 'CustomerIndexCtrl'
                }
            },
            resolve: {

                customers: function ($customers, $ionicNavViewDelegate) {
                    
                    var leave = false;
                    $ionicNavViewDelegate._instances.forEach(function (instance) {
                        var viewElements = instance.getViewElements();
                        for (var x = 0, l = viewElements.length; x < l; x++) {
                            var viewElement = viewElements.eq(x);
                            var eleIdentifier = viewElement.data('$eleId');
                            if ('app.customers' == eleIdentifier) {
                                leave = true;
                            }
                        }
                    });

                    if (!leave) {
                        return $customers.active();
                    }

                }
            }
        })
1 Like

Thanks for sharing! I will look into this and see how this works with $stateParams.

@chrfritsch, i hereby officially announce you my “personal hero of the week” :wink:

I took a little different approach, introducing a custom delegate-id attribute to my views:

<ion-view delegate-id="{{ref.uri}}" view-title="{{ref.name || ref.uri}}">

I had to introduce some kind of identifier, since most of my resolves use $stateParams, so ref.uri uniquely identifies this view instance. I first wanted to use delegate-handle but according to https://github.com/driftyco/ionic/issues/1865, the delegate-handle attribute doesn’t support interpolation :frowning:

This way, searching fo a cached view instance becomes

$ionicNavViewDelegate._instances.forEach(function(instance) {
 var viewElements = instance.getViewElements();
 for (var i = 0, length = viewElements.length; i !== length; ++i) {
  var viewElement = viewElements.eq(i);
  if (viewElement.attr('delegate-id') === $stateParams.uri) {
   leave = true;
  }
 }
});

So, once again, thanks for sharing this!

2 Likes

@chrfritsch Thanks for sharing!

You ar my “personal hero of the week” too :smiley: :smiley: :smiley: