Skip to content

Commit 63a0834

Browse files
committed
feat(goBack): specify how many views to go back
1 parent fbf497a commit 63a0834

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

Diff for: js/angular/controller/navViewController.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,32 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,
221221
};
222222

223223

224-
self.clearCache = function() {
224+
self.clearCache = function(stateIds) {
225225
var viewElements = $element.children();
226-
var viewElement, viewScope;
226+
var viewElement, viewScope, x, l, y, eleIdentifier;
227227

228-
for (var x = 0, l = viewElements.length; x < l; x++) {
228+
for (x = 0, l = viewElements.length; x < l; x++) {
229229
viewElement = viewElements.eq(x);
230+
231+
if (stateIds) {
232+
eleIdentifier = viewElement.data(DATA_ELE_IDENTIFIER);
233+
234+
for (y = 0; y < stateIds.length; y++) {
235+
if (eleIdentifier === stateIds[y]) {
236+
$ionicViewSwitcher.destroyViewEle(viewElement);
237+
}
238+
}
239+
continue;
240+
}
241+
230242
if (navViewAttr(viewElement) == VIEW_STATUS_CACHED) {
231243
$ionicViewSwitcher.destroyViewEle(viewElement);
244+
232245
} else if (navViewAttr(viewElement) == VIEW_STATUS_ACTIVE) {
233246
viewScope = viewElement.scope();
234247
viewScope && viewScope.$broadcast('$ionicView.clearCache');
235248
}
249+
236250
}
237251
};
238252

Diff for: js/angular/service/history.js

+41-5
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,45 @@ function($rootScope, $state, $location, $window, $timeout, $ionicViewSwitcher, $
557557
/**
558558
* @ngdoc method
559559
* @name $ionicHistory#goBack
560+
* @param {number=} backCount Optional negative integer setting how many views to go
561+
* back. By default it'll go back one view by using the value `-1`. To go back two
562+
* views you would use `-2`. If the number goes farther back than the number of views
563+
* in the current history's stack then it'll go to the first view in the current history's
564+
* stack. If the number is zero or greater then it'll do nothing. It also does not
565+
* cross history stacks, meaning it can only go as far back as the current history.
560566
* @description Navigates the app to the back view, if a back view exists.
561567
*/
562-
goBack: function() {
568+
goBack: function(backCount) {
569+
if (isDefined(backCount) && backCount !== -1) {
570+
if (backCount > -1) return;
571+
572+
var currentHistory = viewHistory.histories[this.currentHistoryId()];
573+
var newCursor = currentHistory.cursor + backCount + 1;
574+
if (newCursor < 1) {
575+
newCursor = 1;
576+
}
577+
578+
currentHistory.cursor = newCursor;
579+
setNavViews(currentHistory.stack[newCursor].viewId);
580+
581+
var cursor = newCursor - 1;
582+
var clearStateIds = [];
583+
var fwdView = getViewById(currentHistory.stack[cursor].forwardViewId);
584+
while (fwdView) {
585+
clearStateIds.push(fwdView.stateId || fwdView.viewId);
586+
cursor++;
587+
if (cursor >= currentHistory.stack.length) break;
588+
fwdView = getViewById(currentHistory.stack[cursor].forwardViewId);
589+
}
590+
591+
var self = this;
592+
if (clearStateIds.length) {
593+
$timeout(function() {
594+
self.clearCache(clearStateIds);
595+
}, 600);
596+
}
597+
}
598+
563599
viewHistory.backView && viewHistory.backView.go();
564600
},
565601

@@ -614,10 +650,10 @@ function($rootScope, $state, $location, $window, $timeout, $ionicViewSwitcher, $
614650
* @description Removes all cached views within every {@link ionic.directive:ionNavView}.
615651
* This both removes the view element from the DOM, and destroy it's scope.
616652
*/
617-
clearCache: function() {
653+
clearCache: function(stateIds) {
618654
$timeout(function() {
619655
$ionicNavViewDelegate._instances.forEach(function(instance) {
620-
instance.clearCache();
656+
instance.clearCache(stateIds);
621657
});
622658
});
623659
},
@@ -776,8 +812,8 @@ function($rootScope, $state, $location, $document, $ionicPlatform, $ionicHistory
776812
}
777813
});
778814

779-
$rootScope.$ionicGoBack = function() {
780-
$ionicHistory.goBack();
815+
$rootScope.$ionicGoBack = function(backCount) {
816+
$ionicHistory.goBack(backCount);
781817
};
782818

783819
// Set the document title when a new view is shown

0 commit comments

Comments
 (0)