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

@ -3,8 +3,8 @@ describe('example.personalLog.LogCtrl', function() {
function createNotesCtrl() {
var injector = angular.injector('ng', 'ngMock');
var scope = injector('$rootScope');
scope.$cookies = injector('$cookies');
var scope = injector.get('$rootScope');
scope.$cookies = injector.get('$cookies');
return scope.$new(example.personalLog.LogCtrl);
}

View file

@ -849,12 +849,14 @@ function angularInit(config, document){
function bootstrap(element, modules) {
modules = modules || [];
modules.unshift(ngModule);
createInjector(modules, angularModule)(['$rootScope', '$compile', '$injector', function(scope, compile, injector){
scope.$apply(function() {
element.data('$injector', injector);
compile(element)(scope);
});
}]);
createInjector(modules, angularModule).invoke(null,
['$rootScope', '$compile', '$injector', function(scope, compile, injector){
scope.$apply(function() {
element.data('$injector', injector);
compile(element)(scope);
});
}]
);
}
function angularJsConfig(document) {

View file

@ -22,7 +22,7 @@
*
* // use the injector to kick of your application
* // use the type inference to auto inject arguments, or use implicit injection
* $injector(function($rootScope, $compile, $document){
* $injector.invoke(null, function($rootScope, $compile, $document){
* $compile($document)($rootScope);
* $rootScope.$digest();
* });
@ -60,21 +60,22 @@ function inferInjectionArgs(fn) {
///////////////////////////////////////
/**
* @ngdoc function
* @ngdoc object
* @name angular.module.AUTO.$injector
* @function
*
* @description
*
* `$injector` function is used to retrieve object instances. Object instances are defined by
* {@link angular.module.AUTO.$provide provider}.
* `$injector` is used to retrieve object instances as defined by
* {@link angular.module.AUTO.$provide provider}, instantiate types, invoke methods,
* and load modules.
*
* The following always holds true:
*
* <pre>
* var $injector = angular.injector();
* expect($injector('$injector')).toBe($injector);
* expect($injector(function($injector){
* expect($injector.get('$injector')).toBe($injector);
* expect($injector.invoke(null, function($injector){
* return $injector;
* }).toBe($injector);
* </pre>
@ -108,14 +109,6 @@ function inferInjectionArgs(fn) {
*
* ## Inline
* As an array of injection names, where the last item in the array is the function to call.
*
* @param {string, function()} argument If the `argument` is:
*
* - `string`: Retrieve an instance of a named object. If object does not exist, use the provider to create
* a new instance. Calling the method repeatedly with the same name will always return the same
* instance.
* - `function`: Invoke the function. This is a short hand for `$injector.`{@link #invoke invoke(null, argument)}.
* @return the object instance or the return value of the invoked function.
*/
/**
@ -261,113 +254,116 @@ function inferInjectionArgs(fn) {
function createInjector(modulesToLoad, moduleRegistry) {
var cache = {},
$injector = internalInjector(cache),
providerSuffix = 'Provider',
providerSuffixLength = providerSuffix.length;
providerSuffixLength = providerSuffix.length,
path = [],
$injector;
value('$injector', $injector);
value('$provide', {service: service, factory: factory, value: value});
value('$injector', $injector = {
get: getService,
invoke: invoke,
instantiate: instantiate,
loadModule: loadModule
});
value('$provide', {
service: service,
factory: factory,
value: value
});
loadModule(modulesToLoad);
return $injector;
////////////////////////////////////
function service(name, provider) {
if (isFunction(provider)){
provider = $injector.instantiate(provider);
provider = instantiate(provider);
}
if (!provider.$get) {
throw Error('Providers must define $get factory method.');
}
cache['#' + name + providerSuffix] = provider;
};
function factory(name, factoryFn) { service(name, { $get:factoryFn }); };
function value(name, value) { factory(name, valueFn(value)); };
}
function internalInjector(cache) {
var path = [];
function factory(name, factoryFn) { service(name, { $get:factoryFn }); }
function injector(value) {
switch(typeof value) {
case 'function':
return invoke(null, value);
case 'string':
var instanceKey = '#' + value,
instance = cache[instanceKey];
if (instance !== undefined || cache.hasOwnProperty(instanceKey)) {
return instance;
}
try {
path.unshift(value);
var providerKey = instanceKey + providerSuffix,
provider = cache[providerKey];
if (provider) {
return cache[instanceKey] = invoke(provider, provider.$get);
} else {
throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
}
} finally {
path.shift();
}
case 'object':
if (isArray(value)) {
return invoke(null, value);
}
default:
throw Error('Injector expects name or function.');
function value(name, value) { factory(name, valueFn(value)); }
function getService(value) {
if (typeof value !== 'string') {
throw Error('Service name expected');
}
var instanceKey = '#' + value,
instance = cache[instanceKey];
if (instance !== undefined || cache.hasOwnProperty(instanceKey)) {
return instance;
}
try {
path.unshift(value);
var providerKey = instanceKey + providerSuffix,
provider = cache[providerKey];
if (provider) {
return cache[instanceKey] = invoke(provider, provider.$get);
} else {
throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
}
} finally {
path.shift();
}
}
function invoke(self, fn, locals){
var args = [],
$inject,
length,
key;
if (typeof fn == 'function') {
$inject = inferInjectionArgs(fn);
length = $inject.length;
} else {
if (isArray(fn)) {
$inject = fn;
length = $inject.length;
fn = $inject[--length];
}
assertArgFn(fn, 'fn');
}
function invoke(self, fn, locals){
var args = [],
$inject,
while(length--) {
key = $inject[length];
args.unshift(
locals && locals.hasOwnProperty(key)
? locals[key]
: getService($inject[length], path)
);
}
length,
key;
// Performance optimization: http://jsperf.com/apply-vs-call-vs-invoke
switch (self ? -1 : args.length) {
case 0: return fn();
case 1: return fn(args[0]);
case 2: return fn(args[0], args[1]);
case 3: return fn(args[0], args[1], args[2]);
case 4: return fn(args[0], args[1], args[2], args[3]);
case 5: return fn(args[0], args[1], args[2], args[3], args[4]);
case 6: return fn(args[0], args[1], args[2], args[3], args[4], args[5]);
case 7: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
case 8: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
case 9: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
case 10: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
default: return fn.apply(self, args);
}
}
if (typeof fn == 'function') {
$inject = inferInjectionArgs(fn);
length = $inject.length;
} else {
if (isArray(fn)) {
$inject = fn;
length = $inject.length;
fn = $inject[--length];
}
assertArgFn(fn, 'fn');
}
while(length--) {
key = $inject[length];
args.unshift(
locals && locals.hasOwnProperty(key)
? locals[key]
: injector($inject[length], path)
);
}
switch (self ? -1 : args.length) {
case 0: return fn();
case 1: return fn(args[0]);
case 2: return fn(args[0], args[1]);
case 3: return fn(args[0], args[1], args[2]);
case 4: return fn(args[0], args[1], args[2], args[3]);
case 5: return fn(args[0], args[1], args[2], args[3], args[4]);
case 6: return fn(args[0], args[1], args[2], args[3], args[4], args[5]);
case 7: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
case 8: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
case 9: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]);
case 10: return fn(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9]);
default: return fn.apply(self, args);
}
};
injector.invoke = invoke;
injector.instantiate = function(Type, locals){
var Constructor = function(){},
instance;
Constructor.prototype = Type.prototype;
instance = new Constructor();
return invoke(instance, Type, locals) || instance;
};
injector.loadModule = loadModule;
return injector;
function instantiate(Type, locals){
var Constructor = function(){},
instance;
Constructor.prototype = Type.prototype;
instance = new Constructor();
return invoke(instance, Type, locals) || instance;
}
function loadModule(modulesToLoad){
@ -380,15 +376,10 @@ function createInjector(modulesToLoad, moduleRegistry) {
}
}
if (isFunction(module) || isArray(module)) {
$injector(module);
invoke(null, module);
} else {
assertArgFn(module, 'module');
}
});
}
loadModule(modulesToLoad);
return $injector;
}

View file

@ -783,7 +783,7 @@ window.jasmine && (function(window){
return function(){
var injector = this.$injector;
if (!injector) {
injector = this.$injector = angular.injector('ng', 'ngMock');
injector = this.$injector = angular.injector('ng', 'ngMock');
}
for(var i = 0, ii = blockFns.length; i < ii; i++) {
injector.invoke(this, blockFns[i]);

View file

@ -92,7 +92,7 @@ angular.scenario.Application.prototype.executeAction = function(action) {
}
var element = $window.angular.element($window.document);
var $injector = element.inheritedData('$injector');
$injector(function($browser){
$injector.invoke(null, function($browser){
$browser.notifyWhenNoOutstandingRequests(function() {
action.call(self, $window, _jQuery($window.document));
});

View file

@ -163,7 +163,7 @@ angular.scenario.Runner.prototype.createSpecRunner_ = function(scope) {
*/
angular.scenario.Runner.prototype.run = function(application) {
var self = this;
var $root = angular.injector('ng')('$rootScope');
var $root = angular.injector('ng').get('$rootScope');
angular.extend($root, this);
angular.forEach(angular.scenario.Runner.prototype, function(fn, name) {
$root[name] = angular.bind(self, fn);

View file

@ -103,25 +103,25 @@ angular.scenario.dsl('browser', function() {
api.url = function() {
return this.addFutureAction('$location.url()', function($window, $document, done) {
done(null, $window.angular.injector('ng')('$location').url());
done(null, $window.angular.injector('ng').get('$location').url());
});
};
api.path = function() {
return this.addFutureAction('$location.path()', function($window, $document, done) {
done(null, $window.angular.injector('ng')('$location').path());
done(null, $window.angular.injector('ng').get('$location').path());
});
};
api.search = function() {
return this.addFutureAction('$location.search()', function($window, $document, done) {
done(null, $window.angular.injector('ng')('$location').search());
done(null, $window.angular.injector('ng').get('$location').search());
});
};
api.hash = function() {
return this.addFutureAction('$location.hash()', function($window, $document, done) {
done(null, $window.angular.injector('ng')('$location').hash());
done(null, $window.angular.injector('ng').get('$location').hash());
});
};

View file

@ -143,7 +143,8 @@ function $CompileProvider(){
* - If you are not asking the linking function to clone the template, create the DOM element(s)
* before you send them to the compiler and keep this reference around.
* <pre>
* var scope = angular.injector('ng')(function($rootScope, $compile){
* var $injector = angular.injector('ng');
* var scope = $injector.invoke(null, function($rootScope, $compile){
* var element = $compile('<p>{{total}}</p>')($rootScope);
* });
* </pre>

View file

@ -85,7 +85,7 @@ function $FilterProvider($provide) {
this.$get = ['$injector', function($injector) {
return function(name) {
return $injector(name + suffix);
return $injector.get(name + suffix);
}
}];

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);