Skip to content

Commit

Permalink
feat($ionicActionSheet): add cancelOnStateChange option, default true
Browse files Browse the repository at this point in the history
Closes #1318

BREAKING CHANGE: $ionicActionSheet's default behavior is now to cancel
when the app's state changes.  To disable this behavior, pass
`cancelOnStateChange: false` into $ionicActionSheet.show().
  • Loading branch information
ajoslin committed Jun 11, 2014
1 parent 05dd7b1 commit 087e55f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions js/angular/service/actionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
* the action sheet, or false to keep it opened.
* - `{function=}` `destructiveButtonClicked` Called when the destructive button is clicked.
* Return true to close the action sheet, or false to keep it opened.
* - `{boolean=}` `cancelOnStateChange` Whether to cancel the actionSheet when navigating
* to a new state. Default true.
*
* @returns {function} `hideSheet` A function which, when called, hides & cancels the action sheet.
*/
Expand All @@ -94,14 +96,20 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
buttonClicked: angular.noop,
$deregisterBackButton: angular.noop,
buttons: [],
cancelOnStateChange: true
}, opts || {});


// Compile the template
var element = scope.element = $compile('<ion-action-sheet buttons="buttons"></ion-action-sheet>')(scope);

// Grab the sheet element for animation
var sheetEl = jqLite(element[0].querySelector('.action-sheet-wrapper'));

var stateChangeListenDone = scope.cancelOnStateChange ?
$rootScope.$on('$stateChangeSuccess', function() { scope.cancel(); }) :
angular.noop;

// removes the actionSheet from the screen
scope.removeSheet = function(done) {
if (scope.removed) return;
Expand All @@ -110,6 +118,8 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
sheetEl.removeClass('action-sheet-up');
$document[0].body.classList.remove('action-sheet-open');
scope.$deregisterBackButton();
stateChangeListenDone();
scope.cancel.$scope = null; //see last line

$animate.removeClass(element, 'active', function() {
scope.$destroy();
Expand Down
16 changes: 16 additions & 0 deletions test/unit/angular/service/actionSheet.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,20 @@ describe('Ionic ActionSheet Service', function() {
expect(cancelSpy).toHaveBeenCalled();
}));

it('should cancelOnStateChange by default', inject(function($rootScope) {
var scope = setup();
spyOn(scope, 'cancel');
$rootScope.$broadcast('$stateChangeSuccess');
expect(scope.cancel).toHaveBeenCalled();
}));

it('should not cancelOnStateChange with option as false', inject(function($rootScope) {
var scope = setup({
cancelOnStateChange: false
});
spyOn(scope, 'cancel');
$rootScope.$broadcast('$stateChangeSuccess');
expect(scope.cancel).not.toHaveBeenCalled();
}));

});

0 comments on commit 087e55f

Please sign in to comment.