Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Add a public "has" method to the Controller class, to check the existence of a given controller #14109

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ng/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ function $ControllerProvider() {
var controllers = {},
globals = false;

/**
* @ngdoc method
* @name $controllerProvider#has
* @param {string} name Controller name to check.
*/
this.has = function(name) {
return controllers.hasOwnProperty(name);
};

/**
* @ngdoc method
* @name $controllerProvider#register
Expand Down
15 changes: 15 additions & 0 deletions test/ng/controllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ describe('$controller', function() {
expect(ctrl instanceof BarCtrl).toBe(true);
});

it('should return true when having an existing controller, should return false otherwise', function() {
$controllerProvider.register('FooCtrl', noop);
$controllerProvider.register('BarCtrl', ['dep1', 'dep2', noop]);
$controllerProvider.register({
'BazCtrl': noop,
'QuxCtrl': ['dep1', 'dep2', noop]
});

expect($controllerProvider.has('FooCtrl')).toBe(true);
expect($controllerProvider.has('BarCtrl')).toBe(true);
expect($controllerProvider.has('BazCtrl')).toBe(true);
expect($controllerProvider.has('QuxCtrl')).toBe(true);

expect($controllerProvider.has('UnknownCtrl')).toBe(false);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would basically merge both tests, into something like:

$controllerProvider.register('FooCtrl', noop);

expect($controllerProvider.has('FooCtrl')).toBe(true);
expect($controllerProvider.has('BarCtrl')).toBe(false);

Or if we wanted to be more thorough, something like:

$controllerProvider.register('FooCtrl', noop);
$controllerProvider.register('BarCtrl', ['dep1', 'dep2', noop]);
$controllerProvider.register({
  'BazCtrl': noop,
  'QuxCtrl': ['dep1', 'dep2', noop]
});

expect($controllerProvider.has('FooCtrl')).toBe(true);
expect($controllerProvider.has('BarCtrl')).toBe(true);
expect($controllerProvider.has('BazCtrl')).toBe(true);
expect($controllerProvider.has('QuxCtrl')).toBe(true);

expect($controllerProvider.has('UnknownCtrl')).toBe(false);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what noop is here, should I replace it by {}?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, too much internals 😁
noop is basically angular.noop, which is basically function() {} (a no-op function). You can use noop as a shorthand for an empty constructor function. For this functionality, you don't need to pass any arguments to the controller, nor does the controller need to have any logic.


it('should allow registration of controllers annotated with arrays', function() {
var FooCtrl = function($scope) { $scope.foo = 'bar'; },
Expand Down