aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMisko Hevery2011-10-26 21:36:19 -0700
committerMisko Hevery2011-11-14 16:39:32 -0800
commit9062996a0e6b449467f5b8ec06824086ca87f9cf (patch)
treef2ba7dfea1e0a06c9f25e5bb2e498758e63191b3
parent411c1ae77eaeef1686274e9e4995641a8f83e765 (diff)
downloadangular.js-9062996a0e6b449467f5b8ec06824086ca87f9cf.tar.bz2
feat(injector): support $inject(fn($service){}) function invocation
-rw-r--r--src/Injector.js28
-rw-r--r--test/InjectorSpec.js6
2 files changed, 23 insertions, 11 deletions
diff --git a/src/Injector.js b/src/Injector.js
index 3a7fdeda..624246af 100644
--- a/src/Injector.js
+++ b/src/Injector.js
@@ -33,7 +33,9 @@
* `injector.eager()`
*/
function createInjector(factories) {
- var instanceCache = {$injector: injector};
+ var instanceCache = {
+ $injector: injector
+ };
factories = factories || angularService;
injector.invoke = invoke;
@@ -42,19 +44,23 @@ function createInjector(factories) {
if (factory.$eager)
injector(name);
});
- return injector;
+ return instanceCache.$injector;
function injector(serviceId, path){
- if (!(serviceId in instanceCache)) {
- var factory = factories[serviceId];
- path = path || [];
- path.unshift(serviceId);
- if (!factory) throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
- inferInjectionArgs(factory);
- instanceCache[serviceId] = invoke(null, factory, [], path);
- path.shift();
+ if (typeof serviceId == 'string') {
+ if (!(serviceId in instanceCache)) {
+ var factory = factories[serviceId];
+ path = path || [];
+ path.unshift(serviceId);
+ if (!factory) throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
+ inferInjectionArgs(factory);
+ instanceCache[serviceId] = invoke(null, factory, [], path);
+ path.shift();
+ }
+ return instanceCache[serviceId];
+ } else {
+ return invoke(null, serviceId, path);
}
- return instanceCache[serviceId];
}
function invoke(self, fn, args, path){
diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js
index 46c9897b..6ac17876 100644
--- a/test/InjectorSpec.js
+++ b/test/InjectorSpec.js
@@ -83,6 +83,7 @@ describe('injector', function() {
function fn(a, b, c, d) {
args = [this, a, b, c, d];
+ return a + b + c + d;
}
@@ -99,6 +100,11 @@ describe('injector', function() {
});
+ it('should invoke the passed in function with all of the dependencies as arguments', function(){
+ expect(injector(['a', 'b', fn], [3, 4])).toEqual(10);
+ });
+
+
it('should fail with errors if not function or array', function(){
expect(function(){
injector.invoke({}, {});