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

Commit

Permalink
refactor($injector): move $injector into the providerCache
Browse files Browse the repository at this point in the history
Better than special-casing '$injector' in createInjector.
  • Loading branch information
taralx authored and IgorMinar committed Jul 20, 2012
1 parent 6e2d971 commit e3e8813
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
11 changes: 5 additions & 6 deletions src/auto/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,10 @@ function createInjector(modulesToLoad) {
decorator: decorator
}
},
providerInjector = createInternalInjector(providerCache, function() {
throw Error("Unknown provider: " + path.join(' <- '));
}),
providerInjector = (providerCache.$injector =
createInternalInjector(providerCache, function() {
throw Error("Unknown provider: " + path.join(' <- '));
})),
instanceCache = {},
instanceInjector = (instanceCache.$injector =
createInternalInjector(instanceCache, function(servicename) {
Expand Down Expand Up @@ -489,9 +490,7 @@ function createInjector(modulesToLoad) {
try {
for(var invokeQueue = moduleFn._invokeQueue, i = 0, ii = invokeQueue.length; i < ii; i++) {
var invokeArgs = invokeQueue[i],
provider = invokeArgs[0] == '$injector'
? providerInjector
: providerInjector.get(invokeArgs[0]);
provider = providerInjector.get(invokeArgs[0]);

provider[invokeArgs[1]].apply(provider, invokeArgs[2]);
}
Expand Down
23 changes: 15 additions & 8 deletions test/auto/injectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
describe('injector', function() {
var providers;
var injector;
var providerInjector;

beforeEach(module(function($provide) {
beforeEach(module(function($provide, $injector) {
providers = function(name, factory, annotations) {
$provide.factory(name, extend(factory, annotations||{}));
};
providerInjector = $injector;
}));
beforeEach(inject(function($injector){
injector = $injector;
Expand Down Expand Up @@ -72,6 +74,11 @@ describe('injector', function() {
});


it('should create a new $injector for the run phase', inject(function($injector) {
expect($injector).not.toBe(providerInjector);
}));


describe('invoke', function() {
var args;

Expand Down Expand Up @@ -535,26 +542,26 @@ describe('injector', function() {


it('should decorate the missing service error with module name', function() {
angular.module('TestModule', [], function($injector) {});
angular.module('TestModule', [], function(xyzzy) {});
expect(function() {
createInjector(['TestModule']);
}).toThrow('Unknown provider: $injector from TestModule');
}).toThrow('Unknown provider: xyzzy from TestModule');
});


it('should decorate the missing service error with module function', function() {
function myModule($injector){}
function myModule(xyzzy){}
expect(function() {
createInjector([myModule]);
}).toThrow('Unknown provider: $injector from ' + myModule);
}).toThrow('Unknown provider: xyzzy from ' + myModule);
});


it('should decorate the missing service error with module array function', function() {
function myModule($injector){}
function myModule(xyzzy){}
expect(function() {
createInjector([['$injector', myModule]]);
}).toThrow('Unknown provider: $injector from ' + myModule);
createInjector([['xyzzy', myModule]]);
}).toThrow('Unknown provider: xyzzy from ' + myModule);
});


Expand Down

0 comments on commit e3e8813

Please sign in to comment.