Revert "Revert "removed few key foreach and replaced thime with for loop for performance.""

This reverts commit a29c5e4c7f.
This commit is contained in:
Shyam Seshadri 2010-06-02 15:05:34 -07:00
parent a29c5e4c7f
commit 39312d1fe3
3 changed files with 44 additions and 31 deletions

View file

@ -24,7 +24,8 @@
<input type="text" name="text"/> <input type="text" name="text"/>
<hr/> <hr/>
<ul> <ul>
<li ng-repeat="item in items.$filter('').$orderBy('name')"> <li Xng-repeat="item in items.$filter('').$orderBy('name')"
ng-repeat="item in items">
{{item.name}} <a href="#{{item.name}}">{{item.parts.join(', ')}}</a> {{item.name}} <a href="#{{item.name}}">{{item.parts.join(', ')}}</a>
</li> </li>
</ul> </ul>

View file

@ -53,14 +53,15 @@ function getterFn(path){
var code = 'function (self){\n'; var code = 'function (self){\n';
code += ' var last, fn, type;\n'; code += ' var last, fn, type;\n';
foreach(path.split('.'), function(key) { foreach(path.split('.'), function(key) {
key = (key == 'this') ? '["this"]' : '.' + key;
code += ' if(!self) return self;\n'; code += ' if(!self) return self;\n';
code += ' last = self;\n'; code += ' last = self;\n';
code += ' self = self.' + key + ';\n'; code += ' self = self' + key + ';\n';
code += ' if(typeof self == "function") \n'; code += ' if(typeof self == "function") \n';
code += ' self = function(){ return last.'+key+'.apply(last, arguments); };\n'; code += ' self = function(){ return last'+key+'.apply(last, arguments); };\n';
if (key.charAt(0) == '$') { if (key.charAt(1) == '$') {
// special code for super-imposed functions // special code for super-imposed functions
var name = key.substr(1); var name = key.substr(2);
code += ' if(!self) {\n'; code += ' if(!self) {\n';
code += ' type = angular.Global.typeOf(last);\n'; code += ' type = angular.Global.typeOf(last);\n';
code += ' fn = (angular[type.charAt(0).toUpperCase() + type.substring(1)]||{})["' + name + '"];\n'; code += ' fn = (angular[type.charAt(0).toUpperCase() + type.substring(1)]||{})["' + name + '"];\n';
@ -123,11 +124,13 @@ function createScope(parent, services, existing) {
if (exp !== undefined) { if (exp !== undefined) {
return expressionCompile(exp).apply(instance, slice.call(arguments, 1, arguments.length)); return expressionCompile(exp).apply(instance, slice.call(arguments, 1, arguments.length));
} else { } else {
foreach(evalLists.sorted, function(list) { for ( var i = 0, iSize = evalLists.sorted.length; i < iSize; i++) {
foreach(list, function(eval) { for ( var queue = evalLists.sorted[i],
instance.$tryEval(eval.fn, eval.handler); jSize = queue.length,
}); j= 0; j < jSize; j++) {
}); instance.$tryEval(queue[j].fn, queue[j].handler);
}
}
} }
}, },

View file

@ -24,14 +24,17 @@ angularDirective("ng-eval", function(expression){
angularDirective("ng-bind", function(expression){ angularDirective("ng-bind", function(expression){
return function(element) { return function(element) {
var lastValue, lastError; var lastValue = noop, lastError = noop;
this.$onEval(function() { this.$onEval(function() {
var error, var error,
value = this.$tryEval(expression, function(e){ value = this.$tryEval(expression, function(e){
error = toJson(e); error = toJson(e);
}), }),
isHtml = value instanceof HTML, isHtml,
isDomElement = isElement(value); isDomElement;
if (lastValue === value && lastError == error) return;
isHtml = value instanceof HTML,
isDomElement = isElement(value);
if (!isHtml && !isDomElement && isObject(value)) { if (!isHtml && !isDomElement && isObject(value)) {
value = toJson(value); value = toJson(value);
} }
@ -72,14 +75,14 @@ function compileBindTemplate(template){
}); });
bindTemplateCache[template] = fn = function(element){ bindTemplateCache[template] = fn = function(element){
var parts = [], self = this; var parts = [], self = this;
foreach(bindings, function(fn){ for ( var i = 0; i < bindings.length; i++) {
var value = fn.call(self, element); var value = bindings[i].call(self, element);
if (isElement(value)) if (isElement(value))
value = ''; value = '';
else if (isObject(value)) else if (isObject(value))
value = toJson(value, true); value = toJson(value, true);
parts.push(value); parts.push(value);
}); };
return parts.join(''); return parts.join('');
}; };
} }
@ -107,21 +110,26 @@ var REMOVE_ATTRIBUTES = {
}; };
angularDirective("ng-bind-attr", function(expression){ angularDirective("ng-bind-attr", function(expression){
return function(element){ return function(element){
var lastValue = {};
this.$onEval(function(){ this.$onEval(function(){
foreach(this.$eval(expression), function(bindExp, key) { var values = this.$eval(expression);
var value = compileBindTemplate(bindExp).call(this, element), for(var key in values) {
var value = compileBindTemplate(values[key]).call(this, element),
specialName = REMOVE_ATTRIBUTES[lowercase(key)]; specialName = REMOVE_ATTRIBUTES[lowercase(key)];
if (specialName) { if (lastValue[key] !== value) {
if (element[specialName] = toBoolean(value)) { lastValue[key] = value;
element.attr(specialName, value); if (specialName) {
if (element[specialName] = toBoolean(value)) {
element.attr(specialName, value);
} else {
element.removeAttr(key);
}
(element.data('$validate')||noop)();
} else { } else {
element.removeAttr(key); element.attr(key, value);
} }
(element.data('$validate')||noop)();
} else {
element.attr(key, value);
} }
}, this); };
}, element); }, element);
}; };
}); });
@ -153,17 +161,18 @@ angularWidget("@ng-repeat", function(expression, element){
var children = [], currentScope = this; var children = [], currentScope = this;
this.$onEval(function(){ this.$onEval(function(){
var index = 0, childCount = children.length, childScope, lastElement = reference; var index = 0, childCount = children.length, childScope, lastElement = reference,
foreach(this.$tryEval(rhs, reference), function(value, key){ collection = this.$tryEval(rhs, reference);
for ( var key in collection) {
if (index < childCount) { if (index < childCount) {
// reuse existing child // reuse existing child
childScope = children[index]; childScope = children[index];
childScope[valueIdent] = value; childScope[valueIdent] = collection[key];
if (keyIdent) childScope[keyIdent] = key; if (keyIdent) childScope[keyIdent] = key;
} else { } else {
// grow children // grow children
childScope = template(element.clone(), createScope(currentScope)); childScope = template(element.clone(), createScope(currentScope));
childScope[valueIdent] = value; childScope[valueIdent] = collection[key];
if (keyIdent) childScope[keyIdent] = key; if (keyIdent) childScope[keyIdent] = key;
lastElement.after(childScope.$element); lastElement.after(childScope.$element);
childScope.$index = index; childScope.$index = index;
@ -174,7 +183,7 @@ angularWidget("@ng-repeat", function(expression, element){
childScope.$eval(); childScope.$eval();
lastElement = childScope.$element; lastElement = childScope.$element;
index ++; index ++;
}); };
// shrink children // shrink children
while(children.length > index) { while(children.length > index) {
children.pop().$element.remove(); children.pop().$element.remove();