From 772e32c220193f026c6f4b4674b44ab915e6f0f0 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 6 Oct 2010 17:01:41 -0700 Subject: change ng:controller to create new scope hence allow nesting --- src/Compiler.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/Compiler.js') diff --git a/src/Compiler.js b/src/Compiler.js index 48638bc3..0c80d3fc 100644 --- a/src/Compiler.js +++ b/src/Compiler.js @@ -9,40 +9,42 @@ function Template(priority) { this.children = []; this.inits = []; this.priority = priority; + this.newScope = false; } Template.prototype = { init: function(element, scope) { var inits = {}; - this.collectInits(element, inits); + this.collectInits(element, inits, scope); foreachSorted(inits, function(queue){ - foreach(queue, function(fn){ - fn(scope); - }); + foreach(queue, function(fn) {fn();}); }); }, - collectInits: function(element, inits) { - var queue = inits[this.priority]; + collectInits: function(element, inits, scope) { + var queue = inits[this.priority], childScope = scope; if (!queue) { inits[this.priority] = queue = []; } element = jqLite(element); + if (this.newScope) { + childScope = createScope(scope); + scope.$onEval(childScope.$eval); + } foreach(this.inits, function(fn) { - queue.push(function(scope) { - scope.$tryEval(function(){ - return fn.call(scope, element); + queue.push(function() { + childScope.$tryEval(function(){ + return fn.call(childScope, element); }, element); }); }); - var i, childNodes = element[0].childNodes, children = this.children, paths = this.paths, length = paths.length; for (i = 0; i < length; i++) { - children[i].collectInits(childNodes[paths[i]], inits); + children[i].collectInits(childNodes[paths[i]], inits, childScope); } }, @@ -121,7 +123,8 @@ Compiler.prototype = { element:function(type) {return jqLite(document.createElement(type));}, text:function(text) {return jqLite(document.createTextNode(text));}, descend: function(value){ if(isDefined(value)) descend = value; return descend;}, - directives: function(value){ if(isDefined(value)) directives = value; return directives;} + directives: function(value){ if(isDefined(value)) directives = value; return directives;}, + scope: function(value){ if(isDefined(value)) template.newScope = template.newScope || value ; return template.newScope;} }; try { priority = element.attr('ng:eval-order') || priority || 0; -- cgit v1.2.3