diff options
Diffstat (limited to 'src/Compiler.js')
| -rw-r--r-- | src/Compiler.js | 126 |
1 files changed, 21 insertions, 105 deletions
diff --git a/src/Compiler.js b/src/Compiler.js index 8512f0c3..98e9630c 100644 --- a/src/Compiler.js +++ b/src/Compiler.js @@ -6,44 +6,26 @@ * bind to a new instance of elements. It also provides a list * of child paths which contain child templates */ -function Template(priority) { +function Template() { this.paths = []; this.children = []; - this.inits = []; - this.priority = priority; + this.linkFns = []; this.newScope = false; } Template.prototype = { - attach: function(element, scope) { - var inits = {}; - this.collectInits(element, inits, scope); - forEachSorted(inits, function(queue){ - forEach(queue, function(fn) {fn();}); - }); - }, - - collectInits: function(element, inits, scope) { - var queue = inits[this.priority], childScope = scope; - if (!queue) { - inits[this.priority] = queue = []; - } + link: function(element, scope) { + var childScope = scope; if (this.newScope) { childScope = isFunction(this.newScope) ? scope.$new(this.newScope(scope)) : scope.$new(); element.data($$scope, childScope); } - // TODO(misko): refactor this!!! - // Why are inits even here? - forEach(this.inits, function(fn) { - queue.push(function() { - childScope.$eval(function(){ - try { - return childScope.$service.invoke(childScope, fn, [element]); - } catch (e) { - childScope.$service('$exceptionHandler')(e); - } - }); - }); + forEach(this.linkFns, function(fn) { + try { + childScope.$service.invoke(childScope, fn, [element]); + } catch (e) { + childScope.$service('$exceptionHandler')(e); + } }); var i, childNodes = element[0].childNodes, @@ -51,16 +33,16 @@ Template.prototype = { paths = this.paths, length = paths.length; for (i = 0; i < length; i++) { - children[i].collectInits(jqLite(childNodes[paths[i]]), inits, childScope); + children[i].link(jqLite(childNodes[paths[i]]), childScope); } }, - addInit:function(linkingFn) { + addLinkFn:function(linkingFn) { if (linkingFn) { if (!linkingFn.$inject) linkingFn.$inject = []; - this.inits.push(linkingFn); + this.linkFns.push(linkingFn); } }, @@ -73,7 +55,7 @@ Template.prototype = { }, empty: function() { - return this.inits.length === 0 && this.paths.length === 0; + return this.linkFns.length === 0 && this.paths.length === 0; } }; @@ -211,7 +193,7 @@ Compiler.prototype = { } } } - template = this.templatize(templateElement, index, 0) || new Template(); + template = this.templatize(templateElement, index) || new Template(); return function(scope, cloneConnectFn){ // important!!: we must call our jqLite.clone() since the jQuery one is trying to be smart // and sometimes changes the structure of the DOM. @@ -222,69 +204,12 @@ Compiler.prototype = { element.data($$scope, scope); scope.$element = element; (cloneConnectFn||noop)(element, scope); - template.attach(element, scope); + template.link(element, scope); return scope; }; }, - - /** - * @workInProgress - * @ngdoc directive - * @name angular.directive.ng:eval-order - * @deprecated - * - * @description - * Normally the view is updated from top to bottom. This usually is - * not a problem, but under some circumstances the values for data - * is not available until after the full view is computed. If such - * values are needed before they are computed the order of - * evaluation can be changed using ng:eval-order - * - * @element ANY - * @param {integer|string=} [priority=0] priority integer, or FIRST, LAST constant - * - * @example - * try changing the invoice and see that the Total will lag in evaluation - * @example - <doc:example> - <doc:source> - <div>TOTAL: without ng:eval-order {{ total | currency }}</div> - <div ng:eval-order='LAST'>TOTAL: with ng:eval-order {{ total | currency }}</div> - <table ng:init="items=[{qty:1, cost:9.99, desc:'gadget'}];total=0;"> - <tr> - <td>QTY</td> - <td>Description</td> - <td>Cost</td> - <td>Total</td> - <td></td> - </tr> - <tr ng:repeat="item in items"> - <td><input name="item.qty"/></td> - <td><input name="item.desc"/></td> - <td><input name="item.cost"/></td> - <td>{{item.qty * item.cost | currency}}</td> - <td><a href="" ng:click="items.$remove(item)">X</a></td> - </tr> - <tr> - <td colspan="3"><a href="" ng:click="items.$add()">add</a></td> - <td>{{ total = items.$sum('qty*cost') | currency }}</td> - </tr> - </table> - </doc:source> - <doc:scenario> - it('should check ng:format', function(){ - expect(using('.doc-example-live div:first').binding("total")).toBe('$0.00'); - expect(using('.doc-example-live div:last').binding("total")).toBe('$9.99'); - input('item.qty').enter('2'); - expect(using('.doc-example-live div:first').binding("total")).toBe('$9.99'); - expect(using('.doc-example-live div:last').binding("total")).toBe('$19.98'); - }); - </doc:scenario> - </doc:example> - */ - - templatize: function(element, elementIndex, priority){ + templatize: function(element, elementIndex){ var self = this, widget, fn, @@ -300,17 +225,8 @@ Compiler.prototype = { 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; - } catch (e) { - // for some reason IE throws error under some weird circumstances. so just assume nothing - priority = priority || 0; - } element.addClass(elementNamespace); - if (isString(priority)) { - priority = PRIORITY[uppercase(priority)] || parseInt(priority, 10); - } - template = new Template(priority); + template = new Template(); eachAttribute(element, function(value, name){ if (!widget) { if (widget = self.widgets('@' + name)) { @@ -330,7 +246,7 @@ Compiler.prototype = { descend = false; directives = false; var parent = element.parent(); - template.addInit(widget.call(selfApi, element)); + template.addLinkFn(widget.call(selfApi, element)); if (parent && parent[0]) { element = jqLite(parent[0].childNodes[elementIndex]); } @@ -361,14 +277,14 @@ Compiler.prototype = { fn = directiveFns[name]; if (fn) { element.addClass('ng-directive'); - template.addInit((directiveFns[name]).call(selfApi, value, element)); + template.addLinkFn((directiveFns[name]).call(selfApi, value, element)); } }); } // Process non text child nodes if (descend) { eachNode(element, function(child, i){ - template.addChild(i, self.templatize(child, i, priority)); + template.addChild(i, self.templatize(child, i)); }); } return template.empty() ? null : template; |
