diff options
| author | Misko Hevery | 2010-03-19 22:18:39 -0700 |
|---|---|---|
| committer | Misko Hevery | 2010-03-19 22:18:39 -0700 |
| commit | f6664ed7f6f6dd1f4f9756f57611a316089149cb (patch) | |
| tree | 7c75e0aa500b35fd69a3115e570afc4902f41bfa /src | |
| parent | c3eac13aa7106d099e8f09c39518051ccf939060 (diff) | |
| download | angular.js-f6664ed7f6f6dd1f4f9756f57611a316089149cb.tar.bz2 | |
tests fixed, still missing widgets
Diffstat (limited to 'src')
| -rw-r--r-- | src/Angular.js | 4 | ||||
| -rw-r--r-- | src/Compiler.js | 49 |
2 files changed, 33 insertions, 20 deletions
diff --git a/src/Angular.js b/src/Angular.js index 39a6e91d..8793274c 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -44,6 +44,10 @@ var isVisible = isVisible || function (element) { return jQuery(element).is(":visible"); }; +function isDefined(value){ + return typeof value !== 'undefined'; +} + function log(a, b, c){ var console = window['console']; switch(arguments.length) { diff --git a/src/Compiler.js b/src/Compiler.js index f4d901fb..3c757dd0 100644 --- a/src/Compiler.js +++ b/src/Compiler.js @@ -26,21 +26,21 @@ Template.prototype = { } }, + addInit:function(init) { if (init) { this.inits.push(init); } }, - setExclusiveInit: function(init) { - this.inits = [init]; - this.addInit = noop; - }, - addChild: function(index, template) { this.paths.push(index); this.children.push(template); + }, + + empty: function() { + return this.inits.length == 0 && this.paths.length == 0; } }; @@ -92,19 +92,25 @@ NodeLite.prototype = { }, after: function(element) { - this.element.parentNode.insertBefore(element, this.element.nextSibling); + this.element.parentNode.insertBefore(nodeLite(element).element, this.element.nextSibling); }, attr: function(name, value){ - if (typeof value == 'undefined') { - return this.element.getAttribute(name); + if (isDefined(value)) { + this.element.setAttribute(name, value); } else { - this.element.setAttribute(name); + return this.element.getAttribute(name); + } + }, + + text: function(value) { + if (isDefined(value)) { + this.element.nodeValue = value; } + return this.element.nodeValue; }, isText: function() { return this.element.nodeType == Node.TEXT_NODE; }, - text: function() { return this.element.nodeValue; }, clone: function() { return nodeLite(this.element.cloneNode(true)); } }; @@ -142,7 +148,8 @@ Compiler.prototype = { widgets = self.widgets, recurse = true, exclusive = false, - template; + directiveQueue = [], + template = new Template(); // process markup for text nodes only element.eachTextNode(function(textNode){ @@ -154,35 +161,37 @@ Compiler.prototype = { // Process attributes/directives element.eachAttribute(function(name, value){ var match = name.match(DIRECTIVE), - directive, init; + directive; if (!exclusive && match) { directive = directives[match[1]]; if (directive) { - init = directive.call(self, value, element); - template = template || new Template(); if (directive.exclusive) { - template.setExclusiveInit(init); exclusive = true; - } else { - template.addInit(init); + directiveQueue = []; } - recurse = recurse && init; + directiveQueue.push(bind(self, directive, value, element)); } else { error("Directive '" + match[0] + "' is not recognized."); } } }); + // Execute directives + foreach(directiveQueue, function(directive){ + var init = directive(); + template.addInit(init); + recurse = recurse && init; + }); + // Process non text child nodes if (recurse) { element.eachNode(function(child, i){ var childTemplate = self.templetize(child); if(childTemplate) { - template = template || new Template(); template.addChild(i, childTemplate); } }); } - return template; + return template.empty() ? null : template; } }; |
