Skip to content

Commit 110ff9f

Browse files
committed
feat(modal): $broadcast 'modal.shown/hidden/removed' from parent scope
Closes #243
1 parent 0c52fd2 commit 110ff9f

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Diff for: js/ext/angular/src/service/ionicModal.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
2626
_this.hide();
2727
};
2828

29-
_this.scope.$on('$destroy', function() {
29+
self.scope.$on('$destroy', function() {
3030
$ionicPlatform.offHardwareBackButton(onHardwareBackButton);
3131
});
3232

@@ -36,20 +36,25 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
3636
this.didInitEvents = true;
3737
}
3838

39+
this.scope.$parent.$broadcast('modal.shown', this);
40+
3941
},
4042
// Hide the modal
4143
hide: function() {
4244
var element = angular.element(this.el);
4345
$animate.removeClass(element, this.animation);
4446

4547
ionic.views.Modal.prototype.hide.call(this);
48+
49+
this.scope.$parent.$broadcast('modal.hidden', this);
4650
},
4751

4852
// Remove and destroy the modal scope
4953
remove: function() {
5054
var self = this,
5155
element = angular.element(this.el);
5256
$animate.leave(angular.element(this.el), function() {
57+
self.scope.$parent.$broadcast('modal.removed', self);
5358
self.scope.$destroy();
5459
});
5560
}
@@ -80,7 +85,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
8085
/**
8186
* Load a modal with the given template string.
8287
*
83-
* A new isolated scope will be created for the
88+
* A new isolated scope will be created for the
8489
* modal and the new element will be appended into the body.
8590
*/
8691
fromTemplate: function(templateString, options) {

Diff for: js/ext/angular/test/service/ionicModal.unit.js

+31
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,35 @@ describe('Ionic Modal', function() {
5454

5555
expect(modalInstance.el.classList.contains('active')).toBe(false);
5656
});
57+
58+
it('should broadcast "modal.shown" on show', function() {
59+
var template = '<div class="modal"></div>';
60+
var m = modal.fromTemplate(template, {});
61+
spyOn(m.scope.$parent, '$broadcast');
62+
m.show();
63+
expect(m.scope.$parent.$broadcast).toHaveBeenCalledWith('modal.shown', m);
64+
});
65+
it('should broadcast "modal.hidden" on hide', function() {
66+
var template = '<div class="modal"></div>';
67+
var m = modal.fromTemplate(template, {});
68+
spyOn(m.scope.$parent, '$broadcast');
69+
m.hide();
70+
expect(m.scope.$parent.$broadcast).toHaveBeenCalledWith('modal.hidden', m);
71+
});
72+
it('should broadcast "modal.removed" on remove', inject(function($animate) {
73+
var template = '<div class="modal"></div>';
74+
var m = modal.fromTemplate(template, {});
75+
var broadcastedModal;
76+
var done = false;
77+
78+
//By the time m.remove() is done, our scope will be destroyed. so we have to save the modal
79+
//it gives us
80+
spyOn(m.scope.$parent, '$broadcast').andCallFake(function(e, modal) {
81+
broadcastedModal = modal;
82+
});
83+
spyOn($animate, 'leave').andCallFake(function(el, cb) { cb(); });
84+
85+
m.remove();
86+
expect(broadcastedModal).toBe(m);
87+
}));
5788
});

0 commit comments

Comments
 (0)