Skip to content

Commit 323e2ce

Browse files
hallucynogenycajoslin
hallucynogenyc
authored andcommitted
fix($ionicActionSheet): fix problems with cancel() not being called
Closes #1013, #1576.
1 parent dfdf084 commit 323e2ce

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

js/angular/service/actionSheet.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
6767
* - `{string}` `titleText` The title to show on the action sheet.
6868
* - `{string=}` `cancelText` The text for a 'cancel' button on the action sheet.
6969
* - `{string=}` `destructiveText` The text for a 'danger' on the action sheet.
70-
* - `{function=}` `cancel` Called if the cancel button is pressed or the backdrop is tapped.
70+
* - `{function=}` `cancel` Called if the cancel button is pressed, the backdrop is tapped or
71+
* the hardware back button is pressed.
7172
* - `{function=}` `buttonClicked` Called when one of the non-destructive buttons is clicked,
7273
* with the index of the button that was clicked and the button object. Return true to close
7374
* the action sheet, or false to keep it opened.
@@ -77,26 +78,17 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
7778
show: function(opts) {
7879
var scope = $rootScope.$new(true);
7980

80-
extend(scope, {
81-
cancel: angular.noop,
82-
buttonClicked: angular.noop,
83-
destructiveButtonClicked: angular.noop,
84-
buttons: []
85-
}, opts);
81+
angular.extend(scope, opts);
8682

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

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

93-
var hideSheet = function(didCancel) {
89+
// removes the actionSheet from the screen
90+
var hideSheet = function(h) {
9491
sheetEl.removeClass('action-sheet-up');
95-
if(didCancel) {
96-
$timeout(function(){
97-
opts.cancel();
98-
}, 200);
99-
}
10092

10193
$animate.removeClass(element, 'active', function() {
10294
scope.$destroy();
@@ -107,31 +99,36 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad
10799
scope.$deregisterBackButton && scope.$deregisterBackButton();
108100
};
109101

110-
// Support Android back button to close
102+
// registerBackButtonAction returns a callback to deregister the action
111103
scope.$deregisterBackButton = $ionicPlatform.registerBackButtonAction(
112104
function(){
113-
hideSheet();
105+
scope.cancel(); //
114106
},
115107
PLATFORM_BACK_BUTTON_PRIORITY_ACTION_SHEET
116108
);
117-
109+
110+
// called when the user presses the cancel button
118111
scope.cancel = function() {
119-
hideSheet(true);
112+
hideSheet();
113+
$timeout(function(){
114+
// after the animation is out, call the cancel callback
115+
opts.cancel && opts.cancel();
116+
},200)
120117
};
121118

122119
scope.buttonClicked = function(index) {
123120
// Check if the button click event returned true, which means
124121
// we can close the action sheet
125122
if((opts.buttonClicked && opts.buttonClicked(index, opts.buttons[index])) === true) {
126-
hideSheet(false);
123+
hideSheet();
127124
}
128125
};
129126

130127
scope.destructiveButtonClicked = function() {
131128
// Check if the destructive button click event returned true, which means
132129
// we can close the action sheet
133130
if((opts.destructiveButtonClicked && opts.destructiveButtonClicked()) === true) {
134-
hideSheet(false);
131+
hideSheet();
135132
}
136133
};
137134

0 commit comments

Comments
 (0)