From 110ff9f47583c7f04bcf5b1eebea6d7bd0b25e99 Mon Sep 17 00:00:00 2001 From: Andy Joslin Date: Thu, 13 Feb 2014 14:40:54 -0500 Subject: [PATCH] feat(modal): $broadcast 'modal.shown/hidden/removed' from parent scope Closes #243 --- js/ext/angular/src/service/ionicModal.js | 9 ++++-- .../angular/test/service/ionicModal.unit.js | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/js/ext/angular/src/service/ionicModal.js b/js/ext/angular/src/service/ionicModal.js index 10d253b6c54..4bef1881d0b 100644 --- a/js/ext/angular/src/service/ionicModal.js +++ b/js/ext/angular/src/service/ionicModal.js @@ -26,7 +26,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv _this.hide(); }; - _this.scope.$on('$destroy', function() { + self.scope.$on('$destroy', function() { $ionicPlatform.offHardwareBackButton(onHardwareBackButton); }); @@ -36,6 +36,8 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv this.didInitEvents = true; } + this.scope.$parent.$broadcast('modal.shown', this); + }, // Hide the modal hide: function() { @@ -43,6 +45,8 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv $animate.removeClass(element, this.animation); ionic.views.Modal.prototype.hide.call(this); + + this.scope.$parent.$broadcast('modal.hidden', this); }, // Remove and destroy the modal scope @@ -50,6 +54,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv var self = this, element = angular.element(this.el); $animate.leave(angular.element(this.el), function() { + self.scope.$parent.$broadcast('modal.removed', self); self.scope.$destroy(); }); } @@ -80,7 +85,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv /** * Load a modal with the given template string. * - * A new isolated scope will be created for the + * A new isolated scope will be created for the * modal and the new element will be appended into the body. */ fromTemplate: function(templateString, options) { diff --git a/js/ext/angular/test/service/ionicModal.unit.js b/js/ext/angular/test/service/ionicModal.unit.js index d384b261225..e7637033442 100644 --- a/js/ext/angular/test/service/ionicModal.unit.js +++ b/js/ext/angular/test/service/ionicModal.unit.js @@ -54,4 +54,35 @@ describe('Ionic Modal', function() { expect(modalInstance.el.classList.contains('active')).toBe(false); }); + + it('should broadcast "modal.shown" on show', function() { + var template = ''; + var m = modal.fromTemplate(template, {}); + spyOn(m.scope.$parent, '$broadcast'); + m.show(); + expect(m.scope.$parent.$broadcast).toHaveBeenCalledWith('modal.shown', m); + }); + it('should broadcast "modal.hidden" on hide', function() { + var template = ''; + var m = modal.fromTemplate(template, {}); + spyOn(m.scope.$parent, '$broadcast'); + m.hide(); + expect(m.scope.$parent.$broadcast).toHaveBeenCalledWith('modal.hidden', m); + }); + it('should broadcast "modal.removed" on remove', inject(function($animate) { + var template = ''; + var m = modal.fromTemplate(template, {}); + var broadcastedModal; + var done = false; + + //By the time m.remove() is done, our scope will be destroyed. so we have to save the modal + //it gives us + spyOn(m.scope.$parent, '$broadcast').andCallFake(function(e, modal) { + broadcastedModal = modal; + }); + spyOn($animate, 'leave').andCallFake(function(el, cb) { cb(); }); + + m.remove(); + expect(broadcastedModal).toBe(m); + })); });