Ionic - is there a way to remove the cache in the controller method?

I know how to clear the cache to view:

.state('app.list', { cache : false, url: "/lists/:listId", views: { 'menuContent': { templateUrl: "templates/listDashboard.html", controller: 'listDashboardCtrl' } } }) 

but I need something else - delete the entire cache for the application in the controller method. how to do it?

+6
source share
3 answers

I found a solution, wrap clearCache and ClearHistory in $timeout . Something like that.

 $scope.logout = function(){ $location.path('/signin') $timeout(function () { $ionicHistory.clearCache(); $ionicHistory.clearHistory(); $log.debug('clearing cache') },300) } 

Edit: Change Seconds timeout

+13
source

You can use $ ionicHistory . From the documentation :

ClearCache ()

Deletes all cached views inside each ionNavView. This removes the view element from the DOM, and destroys its area.

In listDashboardCtrl write:

 function listDashboardCtrl($scope, $ionicHistory){ $ionicHistory.clearCache(); } 
+2
source

Well, this is an old problem, but for those who come in 2017 or later, I will explain what is really happening and how to solve it:

Code $ ionicHistory.clearCache ():

 clearCache: function(stateIds) { return $timeout(function() { $ionicNavViewDelegate._instances.forEach(function(instance) { instance.clearCache(stateIds); }); }); } 

So, as you can see, it takes 1 parameter cllaed stateIds, which is an array of stateId. Indeed, I struggled to figure out that stateId is nothing more than stateName.

So let go deeper. The code $ ionicNavView.clearCache, which is used in the line above "instance.clearCache (stateIds)":

 self.clearCache = function(stateIds) { var viewElements = $element.children(); var viewElement, viewScope, x, l, y, eleIdentifier; for (x = 0, l = viewElements.length; x < l; x++) { viewElement = viewElements.eq(x); if (stateIds) { eleIdentifier = viewElement.data(DATA_ELE_IDENTIFIER); for (y = 0; y < stateIds.length; y++) { if (eleIdentifier === stateIds[y]) { $ionicViewSwitcher.destroyViewEle(viewElement); } } continue; } if (navViewAttr(viewElement) == VIEW_STATUS_CACHED) { $ionicViewSwitcher.destroyViewEle(viewElement); } else if (navViewAttr(viewElement) == VIEW_STATUS_ACTIVE) { viewScope = viewElement.scope(); viewScope && viewScope.$broadcast('$ionicView.clearCache'); } } }; 

And, as you can see in the code, this clearCache WILL NOT MISS ALL COLES, instead it destroys all cached views that match the value in the stateIds array. If there is no parameter, an ACTUAL LOOK WILL ONLY pass.

Thus, the solution for this, using only the Ionic method, is to call $ ionicHistory.clearCache () with all your state names in the array as a parameter.

for example: $ ionicHistory.clearCache (['login', 'map', 'home']); I can’t believe that any Ionic developer hasn’t broken into the code before or missed this simple date. I hope someone takes advantage of this, even being late.

UPDATE: just to make it crystal clear, I want to indicate where the error itself is located (if we can call it a mistake), maybe it can be convenient for developers:

self.clearCache = function (stateIds) {

[...]

  var viewElements = $element.children(); 

} What the whole function does is basically:

Get all elements using JQLite Element loop Make sure that the element is equal to one in the StateIds array and destroys it; skip to the next item. Check if the element is saved in the loop or active, and in both cases destroy it. I will not go into it, but while debugging it, I could see that the elements were obtained from var viewElements = $ element.children (); It is not an array of all your views, even cached, intentionally or not, it does not skip all of your states to clear all those that match "ACTIVE" or "CACHED". If you want it to move all your states and destroy all cached views and data that you need to execute, pass the stateIds array parameter.

In addition, there is another strange behavior, because when I debugged it, I saw when the var viewElements array was filled with two elements, and these two elements were from the same state, one allowed "CACHED" to the other converter in " ACTIVE "', even allowing 2 types used in if conditions, the cache was not cleared at all.

I personally believe that this is incorrectly implemented or is widely used incorrectly. The fact is that there many people split their heads, and the developers do not even give this simple explanation.

0
source

Source: https://habr.com/ru/post/985027/


All Articles