Skip to content

Commit

Permalink
feat(goBack): specify how many views to go back
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Apr 22, 2015
1 parent fbf497a commit 63a0834
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
20 changes: 17 additions & 3 deletions js/angular/controller/navViewController.js
Expand Up @@ -221,18 +221,32 @@ function($scope, $element, $attrs, $compile, $controller, $ionicNavBarDelegate,
};


self.clearCache = function() {
self.clearCache = function(stateIds) {
var viewElements = $element.children();
var viewElement, viewScope;
var viewElement, viewScope, x, l, y, eleIdentifier;

for (var x = 0, l = viewElements.length; x < l; x++) {
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');
}

}
};

Expand Down
46 changes: 41 additions & 5 deletions js/angular/service/history.js
Expand Up @@ -557,9 +557,45 @@ function($rootScope, $state, $location, $window, $timeout, $ionicViewSwitcher, $
/**
* @ngdoc method
* @name $ionicHistory#goBack
* @param {number=} backCount Optional negative integer setting how many views to go
* back. By default it'll go back one view by using the value `-1`. To go back two
* views you would use `-2`. If the number goes farther back than the number of views
* in the current history's stack then it'll go to the first view in the current history's
* stack. If the number is zero or greater then it'll do nothing. It also does not
* cross history stacks, meaning it can only go as far back as the current history.
* @description Navigates the app to the back view, if a back view exists.
*/
goBack: function() {
goBack: function(backCount) {
if (isDefined(backCount) && backCount !== -1) {
if (backCount > -1) return;

var currentHistory = viewHistory.histories[this.currentHistoryId()];
var newCursor = currentHistory.cursor + backCount + 1;
if (newCursor < 1) {
newCursor = 1;
}

currentHistory.cursor = newCursor;
setNavViews(currentHistory.stack[newCursor].viewId);

This comment has been minimized.

Copy link
@jacargentina

jacargentina Apr 22, 2015

This line gives me this error:

TypeError: Cannot read property 'viewId' of undefined
    at Object.IonicModule.factory.goBack (ionic.bundle.js:43079)
    at Scope.IonicModule.factory.run.$rootScope.$ionicGoBack (ionic.bundle.js:43316)
    at $parseFunctionCall (ionic.bundle.js:21031)
    at ionic.bundle.js:53408
    at Scope.$get.Scope.$eval (ionic.bundle.js:23087)
    at Scope.$get.Scope.$apply (ionic.bundle.js:23186)
    at HTMLButtonElement.<anonymous> (ionic.bundle.js:53407)
    at HTMLButtonElement.eventHandler (ionic.bundle.js:11700)
    at triggerMouseEvent (ionic.bundle.js:2863)
    at tapClick (ionic.bundle.js:2852)

Using nightly.

This comment has been minimized.

Copy link
@jacargentina

var cursor = newCursor - 1;
var clearStateIds = [];
var fwdView = getViewById(currentHistory.stack[cursor].forwardViewId);
while (fwdView) {
clearStateIds.push(fwdView.stateId || fwdView.viewId);
cursor++;
if (cursor >= currentHistory.stack.length) break;
fwdView = getViewById(currentHistory.stack[cursor].forwardViewId);
}

var self = this;
if (clearStateIds.length) {
$timeout(function() {
self.clearCache(clearStateIds);
}, 600);
}
}

viewHistory.backView && viewHistory.backView.go();
},

Expand Down Expand Up @@ -614,10 +650,10 @@ function($rootScope, $state, $location, $window, $timeout, $ionicViewSwitcher, $
* @description Removes all cached views within every {@link ionic.directive:ionNavView}.
* This both removes the view element from the DOM, and destroy it's scope.
*/
clearCache: function() {
clearCache: function(stateIds) {
$timeout(function() {
$ionicNavViewDelegate._instances.forEach(function(instance) {
instance.clearCache();
instance.clearCache(stateIds);
});
});
},
Expand Down Expand Up @@ -776,8 +812,8 @@ function($rootScope, $state, $location, $document, $ionicPlatform, $ionicHistory
}
});

$rootScope.$ionicGoBack = function() {
$ionicHistory.goBack();
$rootScope.$ionicGoBack = function(backCount) {
$ionicHistory.goBack(backCount);
};

// Set the document title when a new view is shown
Expand Down

0 comments on commit 63a0834

Please sign in to comment.