Skip to content

Commit e106457

Browse files
committed
feat(modal): add .isShown() method to modal instances
Closes #320
1 parent 110ff9f commit e106457

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

js/ext/angular/src/service/ionicModal.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
99
},
1010
// Show the modal
1111
show: function() {
12-
var _this = this;
12+
var self = this;
1313
var element = angular.element(this.el);
14+
15+
self._isShown = true;
16+
1417
if(!element.parent().length) {
1518
element.addClass(this.animation);
1619
$animate.enter(element, angular.element($document[0].body), null, function() {
1720
});
18-
ionic.views.Modal.prototype.show.call(_this);
21+
ionic.views.Modal.prototype.show.call(self);
1922
} else {
2023
$animate.addClass(element, this.animation, function() {
2124
});
2225
}
2326

2427
if(!this.didInitEvents) {
2528
var onHardwareBackButton = function() {
26-
_this.hide();
29+
self.hide();
2730
};
2831

2932
self.scope.$on('$destroy', function() {
@@ -41,6 +44,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
4144
},
4245
// Hide the modal
4346
hide: function() {
47+
this._isShown = false;
4448
var element = angular.element(this.el);
4549
$animate.removeClass(element, this.animation);
4650

@@ -53,10 +57,15 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
5357
remove: function() {
5458
var self = this,
5559
element = angular.element(this.el);
60+
this._isShown = false;
5661
$animate.leave(angular.element(this.el), function() {
5762
self.scope.$parent.$broadcast('modal.removed', self);
5863
self.scope.$destroy();
5964
});
65+
},
66+
67+
isShown: function() {
68+
return !!this._isShown;
6069
}
6170
});
6271

js/ext/angular/test/service/ionicModal.unit.js

+26-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,36 @@ describe('Ionic Modal', function() {
3232
});
3333

3434
timeout.flush();
35+
expect(done).toBe(true);
36+
});
3537

36-
waitsFor(function() {
37-
return done;
38-
}, "Modal should be loaded", 100);
38+
it('should set isShown on show/hide', function() {
39+
var m = modal.fromTemplate('<div class="modal">hello</div>');
40+
expect(m.isShown()).toBe(false);
41+
m.show();
42+
expect(m.isShown()).toBe(true);
43+
m.hide();
44+
expect(m.isShown()).toBe(false);
45+
});
3946

47+
it('should set isShown on remove', function() {
48+
var m = modal.fromTemplate('<div class="modal">hello</div>');
49+
expect(m.isShown()).toBe(false);
50+
m.show();
51+
expect(m.isShown()).toBe(true);
52+
m.remove();
53+
expect(m.isShown()).toBe(false);
4054
});
4155

56+
it('should animate leave and destroy scope on remove', inject(function($animate) {
57+
var m = modal.fromTemplate('<div class="modal"></div>');
58+
spyOn($animate, 'leave').andCallFake(function(el, cb) { cb(); });
59+
spyOn(m.scope, '$destroy');
60+
m.remove();
61+
expect($animate.leave).toHaveBeenCalled();
62+
expect(m.scope.$destroy).toHaveBeenCalled();
63+
}));
64+
4265
it('Should close on hardware back button', function() {
4366
var template = '<div class="modal"></div>';
4467
var modalInstance = modal.fromTemplate(template);

0 commit comments

Comments
 (0)