Skip to content

Commit 087e55f

Browse files
committed
feat($ionicActionSheet): add cancelOnStateChange option, default true
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().
1 parent 05dd7b1 commit 087e55f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
8282
* the action sheet, or false to keep it opened.
8383
* - `{function=}` `destructiveButtonClicked` Called when the destructive button is clicked.
8484
* Return true to close the action sheet, or false to keep it opened.
85+
* - `{boolean=}` `cancelOnStateChange` Whether to cancel the actionSheet when navigating
86+
* to a new state. Default true.
8587
*
8688
* @returns {function} `hideSheet` A function which, when called, hides & cancels the action sheet.
8789
*/
@@ -94,14 +96,20 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
9496
buttonClicked: angular.noop,
9597
$deregisterBackButton: angular.noop,
9698
buttons: [],
99+
cancelOnStateChange: true
97100
}, opts || {});
98101

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

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

109+
var stateChangeListenDone = scope.cancelOnStateChange ?
110+
$rootScope.$on('$stateChangeSuccess', function() { scope.cancel(); }) :
111+
angular.noop;
112+
105113
// removes the actionSheet from the screen
106114
scope.removeSheet = function(done) {
107115
if (scope.removed) return;
@@ -110,6 +118,8 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
110118
sheetEl.removeClass('action-sheet-up');
111119
$document[0].body.classList.remove('action-sheet-open');
112120
scope.$deregisterBackButton();
121+
stateChangeListenDone();
122+
scope.cancel.$scope = null; //see last line
113123

114124
$animate.removeClass(element, 'active', function() {
115125
scope.$destroy();

Diff for: test/unit/angular/service/actionSheet.unit.js

+16
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,20 @@ describe('Ionic ActionSheet Service', function() {
8484
expect(cancelSpy).toHaveBeenCalled();
8585
}));
8686

87+
it('should cancelOnStateChange by default', inject(function($rootScope) {
88+
var scope = setup();
89+
spyOn(scope, 'cancel');
90+
$rootScope.$broadcast('$stateChangeSuccess');
91+
expect(scope.cancel).toHaveBeenCalled();
92+
}));
93+
94+
it('should not cancelOnStateChange with option as false', inject(function($rootScope) {
95+
var scope = setup({
96+
cancelOnStateChange: false
97+
});
98+
spyOn(scope, 'cancel');
99+
$rootScope.$broadcast('$stateChangeSuccess');
100+
expect(scope.cancel).not.toHaveBeenCalled();
101+
}));
102+
87103
});

0 commit comments

Comments
 (0)