aboutsummaryrefslogtreecommitdiffstats
path: root/src/Injector.js
diff options
context:
space:
mode:
authorMisko Hevery2010-10-08 17:30:13 -0700
committerMisko Hevery2010-10-12 16:33:06 -0700
commitd9abfe8a7e488be8725f56077527b16f7c79546a (patch)
tree67089c5d2059e7a56afab0fec19dbce76fdab798 /src/Injector.js
parentfbfd160316de1b99e7afa4102c7fae2ee5b9c1f5 (diff)
downloadangular.js-d9abfe8a7e488be8725f56077527b16f7c79546a.tar.bz2
Introduced injector and $new to scope, and injection into link methods and controllers
- added angular.injector(scope, services, instanceCache) which returns inject - inject method can return, instance, or call function which have $inject property - initialize services with $creation=[eager|eager-publish] this means that only some of the services are now globally accessible - upgraded $become on scope to use injector hence respect the $inject property for injection - $become should not be run multiple times and will most likely be removed in future version - added $new on scope to create a child scope - $inject is respected on constructor function - simplified scopes so that they no longer have separate __proto__ for parent, api, behavior and instance this should speed up execution since scope will now create one __proto__ chain per scope (not three). BACKWARD COMPATIBILITY WARNING: - services now need to have $inject instead of inject property for proper injection this breaks backward compatibility - not all services are now published into root scope (only: $location, $cookie, $window) - if you have widget/directive which uses services on scope (such as this.$xhr), you will now have to inject that service in (as it is not published on the root scope anymore)
Diffstat (limited to 'src/Injector.js')
-rw-r--r--src/Injector.js59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/Injector.js b/src/Injector.js
new file mode 100644
index 00000000..554e9089
--- /dev/null
+++ b/src/Injector.js
@@ -0,0 +1,59 @@
+/**
+ * Create an inject method
+ * @param providerScope provider's "this"
+ * @param providers a function(name) which returns provider function
+ * @param cache place where instances are saved for reuse
+ * @returns {Function}
+ */
+function createInjector(providerScope, providers, cache) {
+ providers = providers || angularService;
+ cache = cache || {};
+ providerScope = providerScope || {};
+ /**
+ * injection function
+ * @param value: string, array, object or function.
+ * @param scope: optional function "this"
+ * @param args: optional arguments to pass to function after injection
+ * parameters
+ * @returns depends on value:
+ * string: return an instance for the injection key.
+ * array of keys: returns an array of instances.
+ * function: look at $inject property of function to determine instances
+ * and then call the function with instances and scope. Any
+ * additional arguments are passed on to function.
+ * object: initialize eager providers and publish them the ones with publish here.
+ * none: same as object but use providerScope as place to publish.
+ */
+ return function inject(value, scope, args){
+ var returnValue, provider, creation;
+ if (isString(value)) {
+ if (!cache.hasOwnProperty(value)) {
+ provider = providers[value];
+ if (!provider) throw "Unknown provider for '"+value+"'.";
+ cache[value] = inject(provider, providerScope);
+ }
+ returnValue = cache[value];
+ } else if (isArray(value)) {
+ returnValue = [];
+ foreach(value, function(name) {
+ returnValue.push(inject(name));
+ });
+ } else if (isFunction(value)) {
+ returnValue = inject(value.$inject || []);
+ returnValue = value.apply(scope, concat(returnValue, arguments, 2));
+ } else if (isObject(value)) {
+ foreach(providers, function(provider, name){
+ creation = provider.$creation;
+ if (creation == 'eager') {
+ inject(name);
+ }
+ if (creation == 'eager-published') {
+ setter(value, name, inject(name));
+ }
+ });
+ } else {
+ returnValue = inject(providerScope);
+ }
+ return returnValue;
+ };
+} \ No newline at end of file