refactor(injector): $injector is no longer a function.

- $injector('abc') -> $injector.get('abc');
- $injector(fn) -> $injector.invoke(null, fn);
This commit is contained in:
Misko Hevery
2011-11-12 15:23:30 -08:00
parent 8d6dc0b9a7
commit e88dfb734a
16 changed files with 182 additions and 179 deletions

View File

@@ -386,7 +386,7 @@ describe('angular', function() {
expect(angular.injector(function($provide){
$provide.factory('svc1', function() { return 'svc1'; });
$provide.factory('svc2', ['svc1', function(s) { return 'svc2-' + s; }]);
})('svc2')).toEqual('svc2-svc1');
}).get('svc2')).toEqual('svc2-svc1');
});
});

View File

@@ -15,16 +15,16 @@ describe('injector', function() {
var instance = {},
original = instance;
providers('instance', function() { return instance; });
expect(injector('instance')).toEqual(instance);
expect(injector.get('instance')).toEqual(instance);
instance = 'deleted';
expect(injector('instance')).toEqual(original);
expect(injector.get('instance')).toEqual(original);
});
it('should inject providers', function() {
providers('a', function() {return 'Mi';});
providers('b', function(mi) {return mi+'sko';}, {$inject:['a']});
expect(injector('b')).toEqual('Misko');
expect(injector.get('b')).toEqual('Misko');
});
@@ -47,7 +47,7 @@ describe('injector', function() {
providers('s5', function() { log.push('s5'); });
providers('s6', function() { log.push('s6'); });
injector('s1');
injector.get('s1');
expect(log).toEqual(['s6', 's5', 's3', 's4', 's2', 's1']);
});
@@ -55,7 +55,7 @@ describe('injector', function() {
it('should provide useful message if no provider', function() {
expect(function() {
injector('idontexist');
injector.get('idontexist');
}).toThrow("Unknown provider for 'idontexist'.");
});
@@ -63,7 +63,7 @@ describe('injector', function() {
providers('a', function(idontexist) {return 1;});
providers('b', function(a) {return 2;});
expect(function() {
injector('b');
injector.get('b');
}).toThrow("Unknown provider for 'idontexist' <- 'a' <- 'b'.");
});
@@ -100,7 +100,7 @@ describe('injector', function() {
it('should invoke the passed in function with all of the dependencies as arguments', function(){
providers('c', function() {return 3;});
providers('d', function() {return 4;});
expect(injector(['a', 'b', 'c', 'd', fn])).toEqual(10);
expect(injector.invoke(null, ['a', 'b', 'c', 'd', fn])).toEqual(10);
});
@@ -163,7 +163,7 @@ describe('injector', function() {
it('should have $injector', function() {
var $injector = createInjector();
expect($injector('$injector')).toBe($injector);
expect($injector.get('$injector')).toBe($injector);
});
it('should define module', function() {
@@ -176,7 +176,7 @@ describe('injector', function() {
});
}, function(valueProvider, fnProvider, serviceProvider) {
log += valueProvider.$get() + fnProvider.$get() + serviceProvider.$get();
}])(function(value, fn, service) {
}]).invoke(null, function(value, fn, service) {
log += '->' + value + fn + service;
});
expect(log).toEqual('value;function;service;->value;function;service;');
@@ -189,8 +189,8 @@ describe('injector', function() {
$injector = createInjector([
angular.extend(function(p) { $provide = p; }, {$inject: ['$provide']})
]);
expect($injector('$injector')).toBe($injector);
expect($injector('$provide')).toBe($provide);
expect($injector.get('$injector')).toBe($injector);
expect($injector.get('$provide')).toBe($provide);
});
@@ -210,9 +210,9 @@ describe('injector', function() {
p.value('c', serviceB.$get() + 'C');
}]
]);
expect($injector('a')).toEqual('A');
expect($injector('b')).toEqual('AB');
expect($injector('c')).toEqual('ABC');
expect($injector.get('a')).toEqual('A');
expect($injector.get('b')).toEqual('AB');
expect($injector.get('c')).toEqual('ABC');
});
@@ -222,7 +222,7 @@ describe('injector', function() {
provide.value('a', 'abc');
}]
});
expect($injector('a')).toEqual('abc');
expect($injector.get('a')).toEqual('abc');
});
it('should error on invalid madule name', function(){
@@ -237,7 +237,7 @@ describe('injector', function() {
it('should configure $provide values', function() {
expect(createInjector([function($provide) {
$provide.value('value', 'abc');
}])('value')).toEqual('abc');
}]).get('value')).toEqual('abc');
});
});
@@ -246,7 +246,7 @@ describe('injector', function() {
it('should configure $provide factory function', function() {
expect(createInjector([function($provide) {
$provide.factory('value', valueFn('abc'));
}])('value')).toEqual('abc');
}]).get('value')).toEqual('abc');
});
});
@@ -257,7 +257,7 @@ describe('injector', function() {
$provide.service('value', {
$get: valueFn('abc')
});
}])('value')).toEqual('abc');
}]).get('value')).toEqual('abc');
});
@@ -269,7 +269,7 @@ describe('injector', function() {
};
expect(createInjector([function($provide) {
$provide.service('value', Type);
}])('value')).toEqual('abc');
}]).get('value')).toEqual('abc');
});
});
});
@@ -318,14 +318,14 @@ describe('injector', function() {
it('should retrieve by name and cache instance', function() {
expect(instance).toEqual({name: 'angular'});
expect($injector('instance')).toBe(instance);
expect($injector('instance')).toBe(instance);
expect($injector.get('instance')).toBe(instance);
expect($injector.get('instance')).toBe(instance);
});
it('should call functions and infer arguments', function() {
expect($injector(function(instance) { return instance; })).toBe(instance);
expect($injector(function(instance) { return instance; })).toBe(instance);
expect($injector.invoke(null, function(instance) { return instance; })).toBe(instance);
expect($injector.invoke(null, function(instance) { return instance; })).toBe(instance);
});
});
@@ -342,7 +342,9 @@ describe('injector', function() {
it('should invoke method', function() {
expect($injector(function(book, author) { return author + ':' + book;})).toEqual('melville:moby');
expect($injector.invoke(null, function(book, author) {
return author + ':' + book;
})).toEqual('melville:moby');
expect($injector.invoke($injector, function(book, author) {
expect(this).toEqual($injector);
return author + ':' + book;})).toEqual('melville:moby');
@@ -350,7 +352,9 @@ describe('injector', function() {
it('should invoke method with locals', function() {
expect($injector(function(book, author) { return author + ':' + book;})).toEqual('melville:moby');
expect($injector.invoke(null, function(book, author) {
return author + ':' + book;
})).toEqual('melville:moby');
expect($injector.invoke($injector,
function(book, author, chapter) {
expect(this).toEqual($injector);
@@ -360,8 +364,9 @@ describe('injector', function() {
it('should invoke method which is annotated', function() {
expect($injector(extend(function(b, a) { return a + ':' + b}, {$inject:['book', 'author']}))).
toEqual('melville:moby');
expect($injector.invoke(null, extend(function(b, a) {
return a + ':' + b
}, {$inject:['book', 'author']}))).toEqual('melville:moby');
expect($injector.invoke($injector, extend(function(b, a) {
expect(this).toEqual($injector);
return a + ':' + b;
@@ -370,7 +375,9 @@ describe('injector', function() {
it('should invoke method which is an array of annotation', function() {
expect($injector(function(book, author) { return author + ':' + book;})).toEqual('melville:moby');
expect($injector.invoke(null, function(book, author) {
return author + ':' + book;
})).toEqual('melville:moby');
expect($injector.invoke($injector, function(book, author) {
expect(this).toEqual($injector);
return author + ':' + book;

View File

@@ -150,7 +150,7 @@ describe("markups", function() {
it('should bind Text with no Bindings', inject(function($compile) {
var $rootScope;
function newScope (){
return $rootScope = angular.injector('ng')('$rootScope');
return $rootScope = angular.injector('ng').get('$rootScope');
}
forEach(['checked', 'disabled', 'multiple', 'readonly', 'selected'], function(name) {
var element = $compile('<div ng:' + name + '="some"></div>')(newScope())

View File

@@ -11,7 +11,7 @@ describe("angular.scenario.dsl", function() {
angular: new angular.scenario.testing.MockAngular()
};
$window.document.data('$injector', $injector);
$root = $injector('$rootScope');
$root = $injector.get('$rootScope');
$root.emit = function(eventName) {
eventLog.push(eventName);
};
@@ -162,16 +162,18 @@ describe("angular.scenario.dsl", function() {
describe('location', function() {
beforeEach(function() {
$window.angular.injector = function() {
return function(serviceId) {
if (serviceId == '$location') {
return {
url: function() {return '/path?search=a#hhh';},
path: function() {return '/path';},
search: function() {return {search: 'a'};},
hash: function() {return 'hhh';}
};
return {
get: function(serviceId) {
if (serviceId == '$location') {
return {
url: function() {return '/path?search=a#hhh';},
path: function() {return '/path';},
search: function() {return {search: 'a'};},
hash: function() {return 'hhh';}
};
}
throw new Error('unknown service id ' + serviceId);
}
throw new Error('unknown service id ' + serviceId);
};
};
});

View File

@@ -285,7 +285,7 @@ describe('Scope', function() {
it('should return a function that allows listeners to be unregistered', inject(function($rootScope) {
var root = angular.injector('ng')('$rootScope'),
var root = angular.injector('ng').get('$rootScope'),
listener = jasmine.createSpy('watch listener'),
listenerRemove;
@@ -470,7 +470,7 @@ describe('Scope', function() {
it('should add listener for both $emit and $broadcast events', inject(function($rootScope) {
var log = '',
root = angular.injector('ng')('$rootScope'),
root = angular.injector('ng').get('$rootScope'),
child = root.$new();
function eventFn() {
@@ -490,7 +490,7 @@ describe('Scope', function() {
it('should return a function that deregisters the listener', inject(function($rootScope) {
var log = '',
root = angular.injector('ng')('$rootScope'),
root = angular.injector('ng').get('$rootScope'),
child = root.$new(),
listenerRemove;
@@ -669,7 +669,7 @@ describe('Scope', function() {
describe('listener', function() {
it('should receive event object', inject(function($rootScope) {
var scope = angular.injector('ng')('$rootScope'),
var scope = angular.injector('ng').get('$rootScope'),
child = scope.$new(),
event;
@@ -685,7 +685,7 @@ describe('Scope', function() {
it('should support passing messages as varargs', inject(function($rootScope) {
var scope = angular.injector('ng')('$rootScope'),
var scope = angular.injector('ng').get('$rootScope'),
child = scope.$new(),
args;

View File

@@ -31,8 +31,8 @@ beforeEach(function() {
afterEach(function() {
if (this.$injector) {
var $rootScope = this.$injector('$rootScope');
var $log = this.$injector('$log');
var $rootScope = this.$injector.get('$rootScope');
var $log = this.$injector.get('$log');
// release the injector
dealoc($rootScope);

View File

@@ -507,15 +507,15 @@ describe("widget", function() {
it('should be possible to nest ng:view in ng:include', inject(function() {
var injector = angular.injector('ng', 'ngMock');
var myApp = injector('$rootScope');
var $browser = injector('$browser');
var myApp = injector.get('$rootScope');
var $browser = injector.get('$browser');
$browser.xhr.expectGET('includePartial.html').respond('view: <ng:view></ng:view>');
injector('$location').path('/foo');
injector.get('$location').path('/foo');
var $route = injector('$route');
var $route = injector.get('$route');
$route.when('/foo', {controller: angular.noop, template: 'viewPartial.html'});
var element = injector('$compile')(
var element = injector.get('$compile')(
'<div>' +
'include: <ng:include src="\'includePartial.html\'"> </ng:include>' +
'</div>')(myApp);