refactor(lifecycle): disconnect leaving scope after transition

Closes #2818
This commit is contained in:
Adam Bradley
2015-02-11 08:37:23 -06:00
parent 3628ebac16
commit 04a0cce134
3 changed files with 38 additions and 3 deletions

View File

@@ -164,9 +164,13 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,
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);
// disconnect the leaving scope
ionic.Utils.disconnectScope(viewElement.scope());
}
}
}

View File

@@ -102,9 +102,6 @@ function($timeout, $document, $q, $ionicClickBlock, $ionicConfig, $ionicNavBarDe
},
render: function(registerData, callback) {
// disconnect the leaving scope before reconnecting or creating a scope for the entering view
leavingEle && ionic.Utils.disconnectScope(leavingEle.scope());
if (alreadyInDom) {
// it was already found in the DOM, just reconnect the scope
ionic.Utils.reconnectScope(enteringEle.scope());

View File

@@ -570,6 +570,40 @@ describe('Ionic nav-view', function() {
expect(divs.eq(0).scope().$$disconnected).toBe(false);
}));
it('should have connected scopes at the time of lifecycle events', inject(function ($state, $q, $timeout, $compile) {
elem.append($compile('<div><ion-nav-view></ion-nav-view></div>')(scope));
$state.go(page1State);
$q.flush();
$timeout.flush();
var beforeEnterDisconnected, afterEnterDisconnected, beforeLeaveDisconnected, afterLeaveDisconnected;
scope.$on('$ionicView.beforeEnter', function(ev, d){
beforeEnterDisconnected = elem.find('ion-nav-view').find('div').eq(1).scope().$$disconnected;
});
scope.$on('$ionicView.afterEnter', function(ev, d){
afterEnterDisconnected = elem.find('ion-nav-view').find('div').eq(1).scope().$$disconnected;
});
scope.$on('$ionicView.beforeLeave', function(ev, d){
beforeLeaveDisconnected = elem.find('ion-nav-view').find('div').eq(0).scope().$$disconnected;
});
scope.$on('$ionicView.afterLeave', function(ev, d){
afterLeaveDisconnected = elem.find('ion-nav-view').find('div').eq(0).scope().$$disconnected;
});
$state.go(page2State);
$q.flush();
$timeout.flush();
expect(beforeEnterDisconnected).toBeUndefined();
expect(afterEnterDisconnected).toBeUndefined();
expect(beforeLeaveDisconnected).toBeUndefined();
expect(afterLeaveDisconnected).toBeUndefined();
expect(elem.find('ion-nav-view').find('div').eq(0).scope().$$disconnected).toBe(true);
expect(elem.find('ion-nav-view').find('div').eq(1).scope().$$disconnected).toBeUndefined();
}));
it('should not cache ion-nav-views that were forward when moving back', inject(function ($state, $q, $timeout, $compile, $ionicConfig) {
elem.append($compile('<div><ion-nav-view></ion-nav-view></div>')(scope));