aboutsummaryrefslogtreecommitdiffstats
path: root/src/Scope.js
diff options
context:
space:
mode:
authorMisko Hevery2010-04-03 17:04:36 -0700
committerMisko Hevery2010-04-03 17:04:36 -0700
commita80a61839a66d244c8bb14bbe2975746e02516c8 (patch)
tree5a7b4d9d3e2a7a15ebf55e068782fbf2aa4ac6bf /src/Scope.js
parent35ca4fcb9c49e505e28669e951e01ddedb01d7db (diff)
downloadangular.js-a80a61839a66d244c8bb14bbe2975746e02516c8.tar.bz2
injection is now working
Diffstat (limited to 'src/Scope.js')
-rw-r--r--src/Scope.js53
1 files changed, 27 insertions, 26 deletions
diff --git a/src/Scope.js b/src/Scope.js
index 26a3f85b..52ab3ed7 100644
--- a/src/Scope.js
+++ b/src/Scope.js
@@ -26,7 +26,7 @@ function getter(instance, path) {
return bind(lastInstance, instance);
}
return instance;
-};
+}
function setter(instance, path, value){
var element = path.split('.');
@@ -41,7 +41,7 @@ function setter(instance, path, value){
}
instance[element.shift()] = value;
return value;
-};
+}
var compileCache = {};
function expressionCompile(exp){
@@ -54,7 +54,7 @@ function expressionCompile(exp){
compileCache[exp] = expFn;
}
return parserNewScopeAdapter(expFn);
-};
+}
// return expFn
// TODO(remove this hack)
@@ -85,21 +85,16 @@ function errorHandlerFor(element, error) {
}
var scopeId = 0;
-function createScope(parent, Class) {
+function createScope(parent, services, existing) {
function Parent(){}
function API(){}
function Behavior(){}
- var instance, behavior, api, evalLists = {};
- if (isFunction(parent)) {
- Class = parent;
- parent = {};
- }
+ var instance, behavior, api, evalLists = {}, servicesCache = extend({}, existing);
- Class = Class || noop;
- parent = Parent.prototype = parent || {};
+ parent = Parent.prototype = (parent || {});
api = API.prototype = new Parent();
- behavior = Behavior.prototype = extend(new API(), Class.prototype);
+ behavior = Behavior.prototype = new API();
instance = new Behavior();
extend(api, {
@@ -161,22 +156,28 @@ function createScope(parent, Class) {
}
});
- if (isUndefined(instance.$root)) {
- behavior.$root = instance;
- behavior.$parent = instance;
+ if (!parent.$root) {
+ api.$root = instance;
+ api.$parent = instance;
}
- (parent.$onEval || noop)(instance.$eval);
- Class.apply(instance, slice.call(arguments, 2, arguments.length));
+ function inject(name){
+ var service = getter(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));
+ });
+ setter(servicesCache, name, service = factory.apply(instance, args));
+ }
+ return service;
+ }
+
+ foreach(services, function(_, name){
+ instance[name] = inject(name);
+ });
return instance;
}
-
-function serviceAdapter(services) {
- return function(){
- var self = this;
- foreach(services, function(service, name){
- self[name] = service.call(self);
- });
- };
-};