Nov 15, 2015

Manually refresh (and clear cache on) current page on IonicView as needed

Ionic v1.2.4

If you've looked around for a solution to manually clear cache and refresh current page, you'll more likely found solution like below.

$ionicHistory.clearCache().then(function () {
  $state.go($state.current, {} , {reload: true});
});


After I've tried the solution above, it doesn't really work as expected, and I don't see how it clear the view cache for current page.
My First try with

$ionicHistory.clearCache().then(function () {
  $state.go($state.current, {} , {reload: true});
});

the page/view would just be redirected with $state.go(), and does not has it cache cleared (it still shows the old content).

As I roughly went through the ionic.bundle.js, I found $ionicHistory.clearCache() actually does accept array value, stateIds, as parameter to clear the view cache specifically.

So, the final solution to the expected result (manually clear cache and refresh current page):

$ionicHistory.clearCache([$state.current.name]).then(function () {
  $state.go($state.current, {}, {reload: true});
});

As you can see, I added the [$state.current.name] to specifically assign each required view cache to be cleared. And so, that's what required for $ionicHistory.clearCache() to work properly.

No comments:

Post a Comment

Hey, thank you for spending time leaving some thoughts, that would be really helpful as encouragement for us to write more quality articles! Thank you!