diff options
| author | Misko Hevery | 2010-10-08 17:30:13 -0700 |
|---|---|---|
| committer | Misko Hevery | 2010-10-12 16:33:06 -0700 |
| commit | d9abfe8a7e488be8725f56077527b16f7c79546a (patch) | |
| tree | 67089c5d2059e7a56afab0fec19dbce76fdab798 /src/Scope.js | |
| parent | fbfd160316de1b99e7afa4102c7fae2ee5b9c1f5 (diff) | |
| download | angular.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/Scope.js')
| -rw-r--r-- | src/Scope.js | 61 |
1 files changed, 19 insertions, 42 deletions
diff --git a/src/Scope.js b/src/Scope.js index 3aaff361..ed608e95 100644 --- a/src/Scope.js +++ b/src/Scope.js @@ -108,20 +108,14 @@ function errorHandlerFor(element, error) { elementError(element, NG_EXCEPTION, isDefined(error) ? toJson(error) : error); } -function createScope(parent, services, existing) { +function createScope(parent, providers, instanceCache) { function Parent(){} - function API(){} - function Behavior(){} - parent = Parent.prototype = (parent || {}); + var instance = new Parent(); var evalLists = {sorted:[]}; var postList = [], postHash = {}, postId = 0; - var servicesCache = extend({}, existing); - var api = API.prototype = new Parent(); - var behavior = Behavior.prototype = new API(); - var instance = new Behavior(); - extend(api, { + extend(instance, { 'this': instance, $id: (scopeId++), $parent: parent, @@ -227,46 +221,29 @@ function createScope(parent, services, existing) { }, $become: function(Class) { - // remove existing - foreach(behavior, function(value, key){ delete behavior[key]; }); - foreach((Class || noop).prototype, function(fn, name){ - behavior[name] = bind(instance, fn); - }); - (Class || noop).call(instance); - - //TODO: backwards compatibility hack, remove when Feedback's init methods are removed - if (behavior.hasOwnProperty('init')) { - behavior.init(); + if (isFunction(Class)) { + instance.constructor = Class; + foreach(Class.prototype, function(fn, name){ + instance[name] = bind(instance, fn); + }); + instance.$inject.apply(instance, concat([Class, instance], arguments, 1)); } + }, + + $new: function(Class) { + var child = createScope(instance); + child.$become.apply(instance, concat([Class], arguments, 1)); + instance.$onEval(child.$eval); + return child; } }); if (!parent.$root) { - api.$root = instance; - api.$parent = instance; - } - - function inject(name){ - var service = servicesCache[name], factory, args = []; - if (isUndefined(service)) { - factory = services[name]; - if (!isFunction(factory)) - throw "Don't know how to inject '" + name + "'."; - foreach(factory.inject, function(dependency){ - args.push(inject(dependency)); - }); - servicesCache[name] = service = factory.apply(instance, args); - } - return service; + instance.$root = instance; + instance.$parent = instance; + (instance.$inject = createInjector(instance, providers, instanceCache))(); } - foreach(services, function(_, name){ - var service = inject(name); - if (service) { - setter(instance, name, service); - } - }); - return instance; } |
