Skip to content

Commit

Permalink
fix(cache): do not cache view after history change
Browse files Browse the repository at this point in the history
If a view is set to not be cached, and it is the first view that was
loaded, and the next navigation was to a different history with its own
ion-nav-views (tabs), then remove the first view. Also allow for the
cache state property to be a string ‘false’ and boolean false.
  • Loading branch information
adamdbradley committed Dec 9, 2014
1 parent 49c861d commit 129e69b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
21 changes: 14 additions & 7 deletions js/angular/controller/navViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,

var DATA_ELE_IDENTIFIER = '$eleId';
var DATA_DESTROY_ELE = '$destroyEle';
var DATA_NO_CACHE = '$noCache';
var VIEW_STATUS_ACTIVE = 'active';
var VIEW_STATUS_CACHED = 'cached';
var HISTORY_AFTER_ROOT = 'after-root';
Expand Down Expand Up @@ -132,26 +133,32 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,

self.transitionEnd = function() {
var viewElements = $element.children();
var viewElementsLength = viewElements.length;
var x, viewElement;
var x, l, viewElement;
var isHistoryRoot;

for (x = 0; x < viewElementsLength; x++) {
for (x = 0, l = viewElements.length; x < l; x++) {
viewElement = viewElements.eq(x);

if (viewElement.data(DATA_ELE_IDENTIFIER) === activeEleId) {
// this is the active element
navViewAttr(viewElement, VIEW_STATUS_ACTIVE);
isHistoryRoot = $ionicViewSwitcher.isHistoryRoot(viewElement);

} else if (navViewAttr(viewElement) === 'leaving' || navViewAttr(viewElement) === VIEW_STATUS_ACTIVE) {
// this is a leaving element or was the former active element
navViewAttr(viewElement, VIEW_STATUS_CACHED);
} else if (navViewAttr(viewElement) === 'leaving' || navViewAttr(viewElement) === VIEW_STATUS_ACTIVE || navViewAttr(viewElement) === VIEW_STATUS_CACHED) {
// this is a leaving element or was the former active element, or is an cached element
if (viewElement.data(DATA_DESTROY_ELE) || viewElement.data(DATA_NO_CACHE)) {
// this element shouldn't stay cached
$ionicViewSwitcher.destroyViewEle(viewElement);
} else {
// keep in the DOM, mark as cached
navViewAttr(viewElement, VIEW_STATUS_CACHED);
}
}
}

if (isHistoryRoot) {
for (x = 0; x < viewElementsLength; x++) {
viewElements = $element.children();
for (x = 0, l = viewElements.length; x < l; x++) {
viewElement = viewElements.eq(x);

if ($ionicViewSwitcher.isHistoryRoot(viewElement) && navViewAttr(viewElement) !== VIEW_STATUS_ACTIVE) {
Expand Down
2 changes: 1 addition & 1 deletion js/angular/service/viewSwitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function($timeout, $document, $q, $ionicClickBlock, $ionicConfig, $ionicNavBarDe

// if the current state has cache:false
// or the element has cache-view="false" attribute
if (viewState(viewLocals).cache === false || enteringEle.attr('cache-view') == 'false') {
if (viewState(viewLocals).cache === false || viewState(viewLocals).cache === 'false' || enteringEle.attr('cache-view') == 'false') {
enteringEle.data(DATA_NO_CACHE, true);
}

Expand Down
30 changes: 29 additions & 1 deletion test/unit/angular/directive/navView.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ describe('Ionic nav-view', function() {
template: '<ion-view>ionViewCacheFalsePropertyState</ion-view>',
cache: false
},
rootView1State = {
cache: 'false',
views: {
'root': {
template: '<ion-view>rootView1State</ion-view>'
}
}
},
tabAbstractState = {
abstract: true,
views: {
Expand Down Expand Up @@ -174,7 +182,8 @@ describe('Ionic nav-view', function() {
.state('tabAbstract.tab1page1', tab1page1State)
.state('tabAbstract.tab2page1', tab2page1State)
.state('tabAbstract.tab3page1', tab3page1State)
.state('tabAbstract.tab3page2', tab3page2NoCacheState);
.state('tabAbstract.tab3page2', tab3page2NoCacheState)
.state('rootView1', rootView1State);
}));

beforeEach(inject(function(_$compile_, $ionicConfig, $rootScope) {
Expand Down Expand Up @@ -932,6 +941,25 @@ describe('Ionic nav-view', function() {
expect(tab3Ele.getAttribute('nav-view')).toBe('cached');
}));

it('should not cache ion-views when going between history and its the first load', inject(function ($state, $q, $timeout, $compile, $ionicConfig) {
elem.append($compile('<ion-nav-view name="root"></ion-nav-view>')(scope));

$state.go(rootView1State);
$q.flush();
$timeout.flush();
expect(elem.find('ion-nav-view').find('ion-view').length).toBe(1);
expect(elem.find('ion-nav-view').find('ion-view').eq(0).text()).toBe('rootView1State');

$state.go(tab1page1State);
$q.flush();
$timeout.flush();

var tab1Ele = elem[0].querySelector('ion-nav-view[name="tab1"]');
expect(tab1Ele.getAttribute('nav-view')).toBe('active');

expect(elem[0].querySelector('ion-nav-view[name="root"]').children.length).toBe(1);
}));

it('should not cache a tab with cache false state property', inject(function ($state, $q, $timeout, $compile, $ionicConfig) {
elem.append($compile('<ion-nav-view name="root"></ion-nav-view>')(scope));

Expand Down

0 comments on commit 129e69b

Please sign in to comment.