From 97fd37e2267d7d39f5ec9bb8e46768b492e9c3d7 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Mon, 19 Oct 2015 08:59:31 -0700 Subject: [PATCH] feat(modal): support $uibModalInstance - Add support for `$uibModalInstance` as a controller local - Deprecate `$modalInstance` support Closes #4638 Closes #4661 --- src/modal/docs/demo.js | 6 +++--- src/modal/docs/readme.md | 2 +- src/modal/modal.js | 15 ++++++++++++--- src/modal/test/modal.spec.js | 33 +++++++++++++++++---------------- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/modal/docs/demo.js b/src/modal/docs/demo.js index 07fba73d75..60e3a5e16b 100644 --- a/src/modal/docs/demo.js +++ b/src/modal/docs/demo.js @@ -34,7 +34,7 @@ angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope // Please note that $modalInstance represents a modal window (instance) dependency. // It is not the same as the $uibModal service used above. -angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) { +angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) { $scope.items = items; $scope.selected = { @@ -42,10 +42,10 @@ angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($s }; $scope.ok = function () { - $modalInstance.close($scope.selected.item); + $uibModalInstance.close($scope.selected.item); }; $scope.cancel = function () { - $modalInstance.dismiss('cancel'); + $uibModalInstance.dismiss('cancel'); }; }); diff --git a/src/modal/docs/readme.md b/src/modal/docs/readme.md index 1bf72279b5..e3c50643bf 100644 --- a/src/modal/docs/readme.md +++ b/src/modal/docs/readme.md @@ -6,7 +6,7 @@ The `$uibModal` service has only one method: `open(options)` where available opt * `templateUrl` - a path to a template representing modal's content * `template` - inline template representing the modal's content * `scope` - a scope instance to be used for the modal's content (actually the `$uibModal` service is going to create a child scope of a provided scope). Defaults to `$rootScope` -* `controller` - a controller for a modal instance - it can initialize scope used by modal. Accepts the "controller-as" syntax in the form 'SomeCtrl as myctrl'; can be injected with `$modalInstance` +* `controller` - a controller for a modal instance - it can initialize scope used by modal. Accepts the "controller-as" syntax in the form 'SomeCtrl as myctrl'; can be injected with `$uibModalInstance` * `controllerAs` - an alternative to the controller-as syntax, matching the API of directive definitions. Requires the `controller` option to be provided as well * `bindToController` - when used with `controllerAs` & set to `true`, it will bind the $scope properties onto the controller directly * `resolve` - members that will be resolved and passed to the controller as locals; it is equivalent of the `resolve` property for AngularJS routes diff --git a/src/modal/modal.js b/src/modal/modal.js index 92d0885297..703ae857d8 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -554,8 +554,8 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap']) backdrop: true, //can also be false or 'static' keyboard: true }, - $get: ['$injector', '$rootScope', '$q', '$templateRequest', '$controller', '$uibModalStack', - function ($injector, $rootScope, $q, $templateRequest, $controller, $modalStack) { + $get: ['$injector', '$rootScope', '$q', '$templateRequest', '$controller', '$uibModalStack', '$modalSuppressWarning', '$log', + function ($injector, $rootScope, $q, $templateRequest, $controller, $modalStack, $modalSuppressWarning, $log) { var $modal = {}; function getTemplatePromise(options) { @@ -641,7 +641,16 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap']) //controllers if (modalOptions.controller) { ctrlLocals.$scope = modalScope; - ctrlLocals.$modalInstance = modalInstance; + ctrlLocals.$uibModalInstance = modalInstance; + Object.defineProperty(ctrlLocals, '$modalInstance', { + get: function() { + if (!$modalSuppressWarning) { + $log.warn('$modalInstance is now deprecated. Use $uibModalInstance instead.'); + } + + return modalInstance; + } + }); angular.forEach(modalOptions.resolve, function(value, key) { ctrlLocals[key] = tplAndVars[resolveIter++]; }); diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index 7b9206133e..a427584548 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -531,9 +531,9 @@ describe('$uibModal', function () { describe('controller', function() { it('should accept controllers and inject modal instances', function() { - var TestCtrl = function($scope, $modalInstance) { + var TestCtrl = function($scope, $uibModalInstance) { $scope.fromCtrl = 'Content from ctrl'; - $scope.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close); + $scope.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close); }; open({template: '
{{fromCtrl}} {{isModalInstance}}
', controller: TestCtrl}); @@ -541,9 +541,9 @@ describe('$uibModal', function () { }); it('should accept controllerAs alias', function() { - $controllerProvider.register('TestCtrl', function($modalInstance) { + $controllerProvider.register('TestCtrl', function($uibModalInstance) { this.fromCtrl = 'Content from ctrl'; - this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close); + this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close); }); open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: 'TestCtrl as test'}); @@ -551,9 +551,9 @@ describe('$uibModal', function () { }); it('should respect the controllerAs property as an alternative for the controller-as syntax', function() { - $controllerProvider.register('TestCtrl', function($modalInstance) { + $controllerProvider.register('TestCtrl', function($uibModalInstance) { this.fromCtrl = 'Content from ctrl'; - this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close); + this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close); }); open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: 'TestCtrl', controllerAs: 'test'}); @@ -561,17 +561,17 @@ describe('$uibModal', function () { }); it('should allow defining in-place controller-as controllers', function() { - open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: function($modalInstance) { + open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: function($uibModalInstance) { this.fromCtrl = 'Content from ctrl'; - this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close); + this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close); }, controllerAs: 'test'}); expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div'); }); it('should allow usage of bindToController', function() { - open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: function($modalInstance) { + open({template: '
{{test.fromCtrl}} {{test.isModalInstance}}
', controller: function($uibModalInstance) { this.fromCtrl = 'Content from ctrl'; - this.isModalInstance = angular.isObject($modalInstance) && angular.isFunction($modalInstance.close); + this.isModalInstance = angular.isObject($uibModalInstance) && angular.isFunction($uibModalInstance.close); }, controllerAs: 'test', bindToController: true}); expect($document).toHaveModalOpenWithContent('Content from ctrl true', 'div'); }); @@ -1201,7 +1201,7 @@ describe('$modal deprecation', function() { inject(function($modal, $timeout, $log, $rootScope) { spyOn($log, 'warn'); - $modal.open({template: '
Foo
'}); + $modal.open({template: '
Foo
', controller: function($modalInstance) {}}); $rootScope.$digest(); $timeout.flush(0); expect($log.warn.calls.count()).toBe(0); @@ -1229,16 +1229,17 @@ describe('$modal deprecation', function() { ''; $templateCache.put('template/modal/window.html', windowTemplate); - $modal.open({template: '
Foo
'}); + $modal.open({template: '
Foo
', controller: function($modalInstance) {}}); $rootScope.$digest(); $timeout.flush(0); - expect($log.warn.calls.count()).toBe(5); + expect($log.warn.calls.count()).toBe(6); expect($log.warn.calls.argsFor(0)).toEqual(['$modal is now deprecated. Use $uibModal instead.']); - expect($log.warn.calls.argsFor(1)).toEqual(['$modalStack is now deprecated. Use $uibModalStack instead.']); - expect($log.warn.calls.argsFor(2)).toEqual(['modal-animation-class is now deprecated. Use uib-modal-animation-class instead.']); + expect($log.warn.calls.argsFor(1)).toEqual(['$modalInstance is now deprecated. Use $uibModalInstance instead.']); + expect($log.warn.calls.argsFor(2)).toEqual(['$modalStack is now deprecated. Use $uibModalStack instead.']); expect($log.warn.calls.argsFor(3)).toEqual(['modal-animation-class is now deprecated. Use uib-modal-animation-class instead.']); - expect($log.warn.calls.argsFor(4)).toEqual(['modal-transclude is now deprecated. Use uib-modal-transclude instead.']); + expect($log.warn.calls.argsFor(4)).toEqual(['modal-animation-class is now deprecated. Use uib-modal-animation-class instead.']); + expect($log.warn.calls.argsFor(5)).toEqual(['modal-transclude is now deprecated. Use uib-modal-transclude instead.']); $log.warn.calls.reset(); $compile('
')($rootScope);