@@ -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